Skip to content

Commit

Permalink
test(core): move create-account integration tests (#3817)
Browse files Browse the repository at this point in the history
* test(core): move update-language tests to bats and new integration

* test(core): move create-account tests to new integration

* test(core): remove redundant 'createAccount' helper
  • Loading branch information
vindard authored Jan 12, 2024
1 parent 49a76db commit 11f678d
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 98 deletions.
26 changes: 26 additions & 0 deletions bats/core/api/user.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load "../../helpers/user.bash"

setup_file() {
create_user 'alice'
}

@test "account: updates language" {
local new_language="de"

exec_graphql 'alice' 'user-details'
language="$(graphql_output '.data.me.language')"
[[ "$language" == "" ]] || exit 1

local variables=$(
jq -n \
--arg language "$new_language" \
'{input: {language: $language}}'
)
exec_graphql 'alice' 'user-update-language' "$variables"
changed_language="$(graphql_output '.data.userUpdateLanguage.user.language')"
[[ "$changed_language" == "$new_language" ]] || exit 1

exec_graphql 'alice' 'user-details'
language="$(graphql_output '.data.me.language')"
[[ "$language" == "$new_language" ]] || exit 1
}
6 changes: 6 additions & 0 deletions bats/gql/user-details.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query userDetails {
me {
id
language
}
}
11 changes: 11 additions & 0 deletions bats/gql/user-update-language.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mutation userUpdateLanguage($input: UserUpdateLanguageInput!) {
userUpdateLanguage(input: $input) {
user {
language
}
errors {
code
message
}
}
}
13 changes: 5 additions & 8 deletions core/api/src/app/accounts/create-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ const initializeCreatedAccount = async ({
config: AccountsConfig
phone?: PhoneNumber
}): Promise<Account | ApplicationError> => {
const newWallet = (currency: WalletCurrency) =>
WalletsRepository().persistNew({
accountId: account.id,
type: WalletType.Checking,
currency,
})

const walletsEnabledConfig = config.initialWallets

// Create all wallets
const enabledWallets: Partial<Record<WalletCurrency, Wallet>> = {}
for (const currency of walletsEnabledConfig) {
const wallet = await newWallet(currency)
const wallet = await WalletsRepository().persistNew({
accountId: account.id,
type: WalletType.Checking,
currency,
})
if (wallet instanceof Error) return wallet
enabledWallets[currency] = wallet
}
Expand Down
2 changes: 2 additions & 0 deletions core/api/test/helpers/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const randomPhone = () =>
`+1415${Math.floor(Math.random() * 9000000 + 1000000)}` as PhoneNumber

export const randomUserId = () => randomUUID() as UserId

export const randomDeviceId = () => randomUUID() as DeviceId
28 changes: 1 addition & 27 deletions core/api/test/helpers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import { Account } from "@/services/mongoose/schema"

import { baseLogger } from "@/services/logger"

import { Accounts, Wallets } from "@/app"
import { Wallets } from "@/app"

import { sleep } from "@/utils"

import { AccountLevel, AccountStatus } from "@/domain/accounts"

const accounts = AccountsRepository()

export const getAccountByPhone = async (phone: PhoneNumber): Promise<Account> => {
Expand Down Expand Up @@ -180,30 +178,6 @@ export const createUserAndWalletFromPhone = async (
}
}

export const createAccount = async ({
initialWallets,
userId,
}: {
initialWallets: WalletCurrency[]
userId?: UserId
}) => {
const phone = randomPhone()

const kratosUserId = userId || randomUserId()

const account = await Accounts.createAccountWithPhoneIdentifier({
newAccountInfo: { phone, kratosUserId },
config: {
initialStatus: AccountStatus.Active,
initialWallets,
initialLevel: AccountLevel.One,
},
})
if (account instanceof Error) throw account

return account
}

export const createRandomUserAndBtcWallet = async () => {
const phone = randomPhone()
return createUserAndWallet(phone)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { Accounts } from "@/app"

import { AccountLevel, AccountStatus } from "@/domain/accounts"
import { WalletCurrency } from "@/domain/shared"

import { AccountsRepository, WalletsRepository } from "@/services/mongoose"

import { createAccount } from "test/helpers"
import { randomPhone, randomUserId } from "test/helpers"

describe("Users - wallets", () => {
describe("with 'createUser'", () => {
describe("createAccountWithPhoneIdentifier", () => {
describe("initialWallets", () => {
it("adds a USD wallet for new user if config is set to true", async () => {
const initialWallets = [WalletCurrency.Btc, WalletCurrency.Usd]

let account: Account | RepositoryError = await createAccount({
initialWallets,
let account = await Accounts.createAccountWithPhoneIdentifier({
newAccountInfo: { phone: randomPhone(), kratosUserId: randomUserId() },
config: {
initialLevel: AccountLevel.One,
initialStatus: AccountStatus.Active,
initialWallets,
},
})
if (account instanceof Error) throw account

// Check all wallets were created
const wallets = await WalletsRepository().listByAccountId(account.id)
Expand All @@ -36,9 +46,16 @@ describe("Users - wallets", () => {
it("does not add a USD wallet for new user if config is set to false", async () => {
const initialWallets = [WalletCurrency.Btc]

let account: Account | RepositoryError = await createAccount({
initialWallets,
})
let account: Account | RepositoryError =
await Accounts.createAccountWithPhoneIdentifier({
newAccountInfo: { phone: randomPhone(), kratosUserId: randomUserId() },
config: {
initialLevel: AccountLevel.One,
initialStatus: AccountStatus.Active,
initialWallets,
},
})
if (account instanceof Error) throw account

// Check all wallets were created
const wallets = await WalletsRepository().listByAccountId(account.id)
Expand All @@ -64,9 +81,16 @@ describe("Users - wallets", () => {
it("sets USD wallet as default if BTC wallet does not exist", async () => {
const initialWallets = [WalletCurrency.Usd]

let account: Account | RepositoryError = await createAccount({
initialWallets,
})
let account: Account | RepositoryError =
await Accounts.createAccountWithPhoneIdentifier({
newAccountInfo: { phone: randomPhone(), kratosUserId: randomUserId() },
config: {
initialLevel: AccountLevel.One,
initialStatus: AccountStatus.Active,
initialWallets,
},
})
if (account instanceof Error) throw account

// Check all wallets were created
const wallets = await WalletsRepository().listByAccountId(account.id)
Expand Down
22 changes: 22 additions & 0 deletions core/api/test/integration/app/user/update-language.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Users } from "@/app"
import { InvalidLanguageError } from "@/domain/errors"

import { createUserAndWalletFromPhone, getUserIdByPhone, randomPhone } from "test/helpers"

let userId: UserId
const phone = randomPhone()

beforeAll(async () => {
await createUserAndWalletFromPhone(phone)
userId = await getUserIdByPhone(phone)
})

describe("UserUpdateLanguage", () => {
it("fails with invalid language", async () => {
const result = await Users.updateLanguage({
userId: userId,
language: "Klingon",
})
expect(result).toBeInstanceOf(InvalidLanguageError)
})
})
48 changes: 0 additions & 48 deletions core/api/test/legacy-integration/app/users/update-language.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Payments, Wallets } from "@/app"
import { Accounts, Payments, Wallets } from "@/app"
import { getMidPriceRatio } from "@/app/prices"
import { getDealerConfig } from "@/config"

import { AccountLevel, AccountStatus } from "@/domain/accounts"
import { toSats } from "@/domain/bitcoin"
import { toCents } from "@/domain/fiat"
import { SubOneCentSatAmountForUsdSelfSendError } from "@/domain/payments"
Expand All @@ -17,7 +18,6 @@ import { AccountsRepository, WalletsRepository } from "@/services/mongoose"
import {
bitcoindClient,
bitcoindOutside,
createAccount,
createMandatoryUsers,
fundLnd,
fundWallet,
Expand All @@ -28,6 +28,8 @@ import {
loadBitcoindWallet,
mineAndConfirm,
openChannelTesting,
randomPhone,
randomUserId,
resetLegacyIntegrationLnds,
} from "test/helpers"

Expand Down Expand Up @@ -128,9 +130,15 @@ if (usdFundingAmount instanceof Error) throw usdFundingAmount

const newAccountAndWallets = async () => {
const initialWallets = [WalletCurrency.Btc, WalletCurrency.Usd]
const account: Account | RepositoryError = await createAccount({
initialWallets,
const account = await Accounts.createAccountWithPhoneIdentifier({
newAccountInfo: { phone: randomPhone(), kratosUserId: randomUserId() },
config: {
initialStatus: AccountStatus.Active,
initialWallets,
initialLevel: AccountLevel.One,
},
})
if (account instanceof Error) throw account

const accountId = account.id
const accountWallets =
Expand Down

0 comments on commit 11f678d

Please sign in to comment.