Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/api/endpoints/addPermission.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ApiAddPermissionParams, callApiAddPermission } from './addPermission'
import { ApiEndpoint, ApiMessageResult, ChainNetwork, ExternalWalletType } from '../../models'
import * as helpers from '../helpers'
import OreIdContext from '../../core/IOreidContext'
import { createOreIdContext } from '../../test-utils'

function isApiAddPermissionParams(obj: any): obj is ApiAddPermissionParams {
return (
'account' in obj &&
obj.account === 'account value' &&
'chainAccount' in obj &&
obj.chainAccount === 'chainAccount value' &&
'chainNetwork' in obj &&
obj.chainNetwork === 'algo_beta' &&
'parentPermission' in obj &&
obj.parentPermission === 'geolocation' &&
'permission' in obj &&
obj.permission === 'geolocation' &&
'provider' in obj &&
obj.provider === 'algosigner' &&
'publicKey' in obj &&
obj.publicKey === 'publicKey value'
)
}

const aapp: ApiAddPermissionParams = {
account: 'account value',
chainAccount: 'chainAccount value',
chainNetwork: ChainNetwork.AlgoBeta,
parentPermission: 'geolocation',
permission: 'geolocation',
provider: ExternalWalletType.AlgoSigner,
publicKey: 'publicKey value',
}

describe('Api Add Permission Params type', () => {
test('type can be instantiated', () => {
expect(isApiAddPermissionParams(aapp)).toBeTruthy()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These instantiation tests aren't needed, they don't any functionality

})
})

let oreIdContext: OreIdContext

beforeEach(() => {
oreIdContext = createOreIdContext()
})

afterEach(() => {
jest.clearAllMocks()
})

describe('Add API permission', () => {
test('should return a valid response', async () => {
const tpSpyKeyOrToken = jest.spyOn(helpers, 'assertHasApiKeyOrAccessToken')
const tpSpyReqdParams = jest.spyOn(helpers, 'assertParamsHaveRequiredValues')
const tpSpyApiCall = jest.spyOn(oreIdContext, 'callOreIdApi')
const resultValue: ApiMessageResult = { success: 'true' }
tpSpyApiCall.mockReturnValue(resultValue as Promise<ApiMessageResult>)
expect(tpSpyKeyOrToken).not.toBeCalled()
expect(tpSpyReqdParams).not.toBeCalled()
expect(tpSpyApiCall).not.toBeCalled()
const result: ApiMessageResult = await callApiAddPermission(oreIdContext, aapp)
expect(tpSpyKeyOrToken).toBeCalledWith(oreIdContext, ApiEndpoint.AddPermission)
expect(tpSpyApiCall).toBeCalled()
expect(result).toBeTruthy()
expect(result.success).toBeDefined()
expect(result.success).toEqual('true')
})
})
58 changes: 58 additions & 0 deletions src/api/endpoints/appToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ApiGetAppTokenParams, callApiGetAppToken } from './appToken'
import {
ApiEndpoint,
AppAccessTokenMetadata,
RequestType,
ApiKeyUsedFor,
NewAccountAppTokenParams,
AccountType,
} from '../../models'
import { assertHasApiKey } from '../helpers'
import Helpers from '../../utils/helpers'
import * as helpers from '../helpers'
import OreIdContext from '../../core/IOreidContext'
import { createOreIdContext } from '../../test-utils'

function isApiGetAppTokenParams(obj: any): obj is ApiGetAppTokenParams {
return 'appAccessTokenMetadata' in obj && typeof obj.appAccessTokenMetadata === 'object'
}

const aatmd: AppAccessTokenMetadata = {}

const agatp: ApiGetAppTokenParams = {
appAccessTokenMetadata: aatmd,
}

describe('Api Get App Token Params type', () => {
test('type can be instantiated', () => {
expect(isApiGetAppTokenParams(agatp)).toBeTruthy()
})
})

let oreIdContext: OreIdContext

beforeEach(() => {
oreIdContext = createOreIdContext()
})

afterEach(() => {
jest.clearAllMocks()
})

describe('Get app token', () => {
test('should return a valid response', async () => {
const tpNullOrEmpty = jest.spyOn(Helpers, 'isNullOrEmpty')
const tpSpyReqdParams = jest.spyOn(helpers, 'assertHasApiKey')
const tpSpyApiCall = jest.spyOn(oreIdContext, 'callOreIdApi')
const resultValue = { appAccessToken: 'appAccessToken value' }
tpSpyApiCall.mockReturnValue(resultValue as unknown as Promise<string>)
expect(tpNullOrEmpty).not.toBeCalled()
expect(tpSpyReqdParams).not.toBeCalled()
expect(tpSpyApiCall).not.toBeCalled()
const result: string = await callApiGetAppToken(oreIdContext, agatp)
expect(tpNullOrEmpty).toBeCalledWith(agatp.appAccessTokenMetadata)
expect(tpSpyReqdParams).toBeCalledWith(oreIdContext, null, ApiEndpoint.AppToken)
expect(tpSpyApiCall).toBeCalled()
expect(result).toEqual('appAccessToken value')
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/canAutoSign.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiCanAutosignTransaction } from './canAutoSign'

describe('Can auto sign', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/convertOauth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiConvertOauthTokens } from './convertOauth'

describe('Convert OAuth', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/custodialMigrateAccount.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiCustodialMigrateAccount } from './custodialMigrateAccount'

describe('Custodial migrate account', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/custodialNewAccount.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiCustodialNewAccount } from './custodialNewAccount'

describe('Custodial new account', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/deleteTestUser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiDeleteTestUser } from './deleteTestUser'

describe('Delete test user', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/getConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiGetConfig } from './getConfig'

describe('Get config', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/getUser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiGetUser } from './getUser'

describe('Get user', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
7 changes: 7 additions & 0 deletions src/api/endpoints/loginUserWithToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { callApiLoginUserWithToken } from './loginUserWithToken'

describe('Login user with token', () => {
test('should return a valid response', async () => {
expect(1).toEqual(1)
})
})
200 changes: 200 additions & 0 deletions src/api/endpoints/newUserWithToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { ApiNewUserWithTokenParams, ApiNewUserWithTokenBodyParams, callApiNewUserWithToken } from './newUserWithToken'
import { ApiEndpoint, ApiMessageResult, ApiResultWithErrorCode, AuthProvider } from '../../models'
import * as helpers from '../helpers'
import OreIdContext from '../../core/IOreidContext'
import { createOreIdContext } from '../../test-utils'

function isApiNewUserWithTokenParams(obj: any): obj is ApiNewUserWithTokenParams {
return (
'accessToken' in obj &&
obj.accessToken === 'abc' &&
'delayWalletSetup' in obj &&
obj.delayWalletSetup &&
'isTestUser' in obj &&
obj.isTestUser &&
'idToken' in obj &&
obj.idToken === 'def' &&
'provider' in obj &&
obj.provider === 'custodial'
)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed


const anuwtp: ApiNewUserWithTokenParams = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand these variable names out

accessToken: 'abc',
delayWalletSetup: true,
isTestUser: true,
idToken: 'def',
provider: AuthProvider.Custodial,
}

const anuwtpNoId: ApiNewUserWithTokenParams = {
accessToken: 'abc',
provider: AuthProvider.Custodial,
}

const anuwtpNoToken: ApiNewUserWithTokenParams = {
idToken: 'def',
}

const anuwtpIdAndToken: ApiNewUserWithTokenParams = {
accessToken: 'abc',
provider: AuthProvider.Custodial,
}

const anuwtpNoProvider: ApiNewUserWithTokenParams = {
accessToken: 'abc',
provider: null,
}

describe('Api New User With Token Params type', () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checking that a constant you defined satisfies the same constant shape you defined in the helper which will always return true. So this test doesn't test any functionality, can you remove it?

test('type can be instantiated', () => {
expect(isApiNewUserWithTokenParams(anuwtp)).toBeTruthy()
})
})

function isApiNewUserWithTokenBodyParams(obj: any): obj is ApiNewUserWithTokenBodyParams {
return (
'access_token' in obj &&
obj.access_token === 'access_token value' &&
'delay_wallet_setup' in obj &&
obj.delay_wallet_setup &&
'is_test_user' in obj &&
obj.is_test_user &&
'id_token' in obj &&
obj.id_token === 'id_token value' &&
'provider' in obj &&
obj.provider === 'provider value'
)
}

const anuwtbp: ApiNewUserWithTokenBodyParams = {
access_token: 'access_token value',
delay_wallet_setup: true,
is_test_user: true,
id_token: 'id_token value',
provider: 'provider value',
}

describe('Api New User With Token Body Params', () => {
test('type can be instantiated', () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as Comment above

expect(isApiNewUserWithTokenBodyParams(anuwtbp)).toBeTruthy()
})
})

let oreIdContext: OreIdContext

beforeEach(() => {
oreIdContext = createOreIdContext()
})

afterEach(() => {
jest.clearAllMocks()
})

describe('Add new user with id token but no access token', () => {
test('should return a valid access token', async () => {
const tpSpyAtLeastOneValue = jest.spyOn(helpers, 'assertParamsHaveAtLeastOneOfValues')
const tpSpyOnlyOneValue = jest.spyOn(helpers, 'assertParamsHaveOnlyOneOfValues')
const tpSpyReqdValues = jest.spyOn(helpers, 'assertParamsHaveRequiredValues')
const tpSpyHasAPIKey = jest.spyOn(helpers, 'assertHasApiKey')
const tpSpyApiCall = jest.spyOn(oreIdContext, 'callOreIdApi')
const resultValue = { accessToken: '', idToken: '' }

tpSpyApiCall.mockReturnValue(resultValue as unknown as Promise<{ accessToken: string } & ApiResultWithErrorCode>)
expect(tpSpyAtLeastOneValue).not.toBeCalled()
expect(tpSpyOnlyOneValue).not.toBeCalled()
expect(tpSpyReqdValues).not.toBeCalled()
expect(tpSpyHasAPIKey).not.toBeCalled()
expect(tpSpyApiCall).not.toBeCalled()

const result = await callApiNewUserWithToken(oreIdContext, anuwtpNoToken)

expect(tpSpyAtLeastOneValue).toBeCalledWith(anuwtpNoToken, ['idToken', 'accessToken'], ApiEndpoint.NewUserWithToken)
expect(tpSpyOnlyOneValue).toBeCalledWith(anuwtpNoToken, ['idToken', 'provider'], ApiEndpoint.NewUserWithToken)
expect(tpSpyReqdValues).not.toBeCalled()
expect(tpSpyHasAPIKey).not.toBeCalled()
expect(tpSpyApiCall).toBeCalled()

expect(result).toBeTruthy()
expect(result.accessToken).toBeDefined()
//expect(result.idToken).toBeDefined()
expect(result).toEqual(resultValue)
})
})

describe('Add new user with access token but no id token', () => {
test('should return a valid access token', async () => {
const tpSpyAtLeastOneValue = jest.spyOn(helpers, 'assertParamsHaveAtLeastOneOfValues')
const tpSpyOnlyOneValue = jest.spyOn(helpers, 'assertParamsHaveOnlyOneOfValues')
const tpSpyReqdValues = jest.spyOn(helpers, 'assertParamsHaveRequiredValues')
const tpSpyHasAPIKey = jest.spyOn(helpers, 'assertHasApiKey')
const tpSpyApiCall = jest.spyOn(oreIdContext, 'callOreIdApi')
const resultValue = { accessToken: '', idToken: '' }

tpSpyApiCall.mockReturnValue(resultValue as unknown as Promise<{ accessToken: string } & ApiResultWithErrorCode>)
expect(tpSpyAtLeastOneValue).not.toBeCalled()
expect(tpSpyOnlyOneValue).not.toBeCalled()
expect(tpSpyReqdValues).not.toBeCalled()
expect(tpSpyHasAPIKey).not.toBeCalled()
expect(tpSpyApiCall).not.toBeCalled()

const result = await callApiNewUserWithToken(oreIdContext, anuwtpNoId)

expect(tpSpyAtLeastOneValue).toBeCalledWith(anuwtpNoId, ['idToken', 'accessToken'], ApiEndpoint.NewUserWithToken)
expect(tpSpyOnlyOneValue).toBeCalledWith(anuwtpNoId, ['idToken', 'provider'], ApiEndpoint.NewUserWithToken)
expect(tpSpyReqdValues).toBeCalledWith(anuwtpNoId, ['accessToken', 'provider'], ApiEndpoint.NewUserWithToken)
expect(tpSpyHasAPIKey).toBeCalledWith(oreIdContext, null, 'new-user-with-token')
expect(tpSpyApiCall).toBeCalled()

expect(result).toBeTruthy()
expect(result.accessToken).toBeDefined()
//expect(result.idToken).toBeDefined()
expect(result).toEqual(resultValue)
})
})

describe('Add new user with access token and id token', () => {
test('should return a valid access token', async () => {
const tpSpyAtLeastOneValue = jest.spyOn(helpers, 'assertParamsHaveAtLeastOneOfValues')
const tpSpyOnlyOneValue = jest.spyOn(helpers, 'assertParamsHaveOnlyOneOfValues')
const tpSpyReqdValues = jest.spyOn(helpers, 'assertParamsHaveRequiredValues')
const tpSpyHasAPIKey = jest.spyOn(helpers, 'assertHasApiKey')
const tpSpyApiCall = jest.spyOn(oreIdContext, 'callOreIdApi')
const resultValue = { accessToken: '', idToken: '' }

tpSpyApiCall.mockReturnValue(resultValue as unknown as Promise<{ accessToken: string } & ApiResultWithErrorCode>)
expect(tpSpyAtLeastOneValue).not.toBeCalled()
expect(tpSpyOnlyOneValue).not.toBeCalled()
expect(tpSpyReqdValues).not.toBeCalled()
expect(tpSpyHasAPIKey).not.toBeCalled()
expect(tpSpyApiCall).not.toBeCalled()

const result = await callApiNewUserWithToken(oreIdContext, anuwtpIdAndToken)

expect(tpSpyAtLeastOneValue).toBeCalledWith(
anuwtpIdAndToken,
['idToken', 'accessToken'],
ApiEndpoint.NewUserWithToken,
)
expect(tpSpyOnlyOneValue).toBeCalledWith(anuwtpIdAndToken, ['idToken', 'provider'], ApiEndpoint.NewUserWithToken)
expect(tpSpyReqdValues).toBeCalledWith(anuwtpIdAndToken, ['accessToken', 'provider'], ApiEndpoint.NewUserWithToken)
expect(tpSpyApiCall).toBeCalled()

expect(result).toBeTruthy()
expect(result.accessToken).toBeDefined()
//expect(result.idToken).toBeDefined()
expect(result).toEqual(resultValue)
})
})

describe('Add new user with access token but no provider', () => {
test('should return an error stating that a provider must be provided', async () => {
const tpSpyAtLeastOneValue = jest.spyOn(helpers, 'assertParamsHaveAtLeastOneOfValues')

// eslint-disable-next-line jest/valid-expect-in-promise
callApiNewUserWithToken(oreIdContext, anuwtpNoProvider).catch(() => {
// eslint-disable-next-line jest/no-conditional-expect
expect(tpSpyAtLeastOneValue).toThrowError()
})
})
})
Loading