Skip to content

Commit

Permalink
default entities are fixture. Added a member and user groups member t…
Browse files Browse the repository at this point in the history
…o fixture
  • Loading branch information
Ptroger committed Aug 30, 2024
1 parent e185f87 commit 588fc61
Show file tree
Hide file tree
Showing 11 changed files with 733 additions and 523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import {
} from '../../util/setup'

const systemManagerHexPk = FIXTURE.UNSAFE_PRIVATE_KEY.Root
const bobPrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Bob
const antoinePrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Antoine
const alicePrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Alice
const davePrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Dave
const bobPrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Bob

describe('checkApprovals', () => {
describe('entity type', () => {
Expand Down Expand Up @@ -55,7 +56,7 @@ describe('checkApprovals', () => {
})

it('get an accessToken after approval from an admin', async () => {
const { authClient } = await buildAuthClient(bobPrivateKey, {
const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})
Expand Down Expand Up @@ -110,15 +111,18 @@ describe('checkApprovals', () => {
})
})

let authId: string

it('is still unauthorized after an admin approval', async () => {
expect.assertions(3)
expect.assertions(2)

const { authClient } = await buildAuthClient(bobPrivateKey, {
const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})

const { decision, authId } = await authClient.authorize(genNonce(request))
const { decision, authId: reqId } = await authClient.authorize(genNonce(request))
authId = reqId
expect(decision).toBe(Decision.CONFIRM)

const { authClient: adminClient } = await buildAuthClient(alicePrivateKey, {
Expand All @@ -133,13 +137,41 @@ describe('checkApprovals', () => {
} catch (e: any) {
expect(e.message).toEqual('Unauthorized')
}
})

it("doesn't authorize if same admin approves twice", async () => {
expect.assertions(1)
const { authClient: adminClient } = await buildAuthClient(alicePrivateKey, {
host: getAuthHost(),
clientId
})

const { authClient: secondAdminClient } = await buildAuthClient(davePrivateKey, {
const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})

await secondAdminClient.approve(authId)
await adminClient.approve(authId)

try {
const accessToken = await authClient.getAccessToken(authId)
} catch (e: any) {
expect(e.message).toEqual('Unauthorized')
}
})

it('is authorized after a second admin approval', async () => {
const { authClient: adminClient } = await buildAuthClient(bobPrivateKey, {
host: getAuthHost(),
clientId
})

await adminClient.approve(authId)

const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})

const accessToken = await authClient.getAccessToken(authId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,97 +14,99 @@ import {
} from '../../util/setup'

const systemManagerHexPk = FIXTURE.UNSAFE_PRIVATE_KEY.Root
const bobPrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Bob
const antoinePrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Antoine
const alicePrivateKey = FIXTURE.UNSAFE_PRIVATE_KEY.Alice

describe('rate limiting by principal', () => {
const request: Request = {
action: Action.SIGN_TRANSACTION,
nonce: 'test-nonce-1',
transactionRequest: {
from: '0x0301e2724a40E934Cce3345928b88956901aA127',
to: '0x76d1b7f9b3F69C435eeF76a98A415332084A856F',
value: '0xde0b6b3a7640000',
chainId: 1
},
resourceId: 'eip155:eoa:0x0301e2724a40e934cce3345928b88956901aa127'
}
// Generate a new client ID for each test run, otherwise historical data with persist between tests if using a long-lived db.
const clientId = v4()

beforeAll(async () => {
const entities = entitiesSchema.parse(defaultEntities)

await createClient(systemManagerHexPk, {
clientId,
authHost: getAuthHost(),
authAdminApiKey: getAuthAdminApiKey()
describe('checkRateLimit', () => {
describe('rate limiting by principal', () => {
const request: Request = {
action: Action.SIGN_TRANSACTION,
nonce: 'test-nonce-1',
transactionRequest: {
from: '0x0301e2724a40E934Cce3345928b88956901aA127',
to: '0x76d1b7f9b3F69C435eeF76a98A415332084A856F',
value: '0xde0b6b3a7640000',
chainId: 1
},
resourceId: 'eip155:eoa:0x0301e2724a40e934cce3345928b88956901aa127'
}
// Generate a new client ID for each test run, otherwise historical data with persist between tests if using a long-lived db.
const clientId = v4()

beforeAll(async () => {
const entities = entitiesSchema.parse(defaultEntities)

await createClient(systemManagerHexPk, {
clientId,
authHost: getAuthHost(),
authAdminApiKey: getAuthAdminApiKey()
})

const policies = buildPolicy([adminPermitAll, memberTwoTransferPerDay])

await saveDataStore(systemManagerHexPk, {
clientId,
host: getAuthHost(),
entities,
policies
})
})

const policies = buildPolicy([adminPermitAll, memberTwoTransferPerDay])

await saveDataStore(systemManagerHexPk, {
clientId,
host: getAuthHost(),
entities,
policies
})
})
it('alice-admin does a transfer that is not counted against the rate limit', async () => {
const { authClient } = await buildAuthClient(alicePrivateKey, {
host: getAuthHost(),
clientId
})

it('alice-admin does a transfer that is not counted against the rate limit', async () => {
const { authClient } = await buildAuthClient(alicePrivateKey, {
host: getAuthHost(),
clientId
const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})

const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})
it('permits member antoine to do a first transfer', async () => {
// First transfer
const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})

it('permits member bob to do a first transfer', async () => {
// First transfer
const { authClient } = await buildAuthClient(bobPrivateKey, {
host: getAuthHost(),
clientId
const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})

const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})
it('permits member antoine to do a second transfer', async () => {
// Second transfer
const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})

it('permits member bob to do a second transfer', async () => {
// Second transfer
const { authClient } = await buildAuthClient(bobPrivateKey, {
host: getAuthHost(),
clientId
const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})

const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})

it('forbids member bob to do a third transfer', async () => {
expect.assertions(1)
// Third transfer
const { authClient } = await buildAuthClient(bobPrivateKey, {
host: getAuthHost(),
clientId
it('forbids member antoine to do a third transfer', async () => {
expect.assertions(1)
// Third transfer
const { authClient } = await buildAuthClient(antoinePrivateKey, {
host: getAuthHost(),
clientId
})

try {
await authClient.requestAccessToken(genNonce(request))
} catch (error: any) {
expect(error.message).toEqual('Unauthorized')
}
})

try {
await authClient.requestAccessToken(genNonce(request))
} catch (error: any) {
expect(error.message).toEqual('Unauthorized')
}
})
it('permits admin alice to do a transfer', async () => {
const { authClient } = await buildAuthClient(alicePrivateKey, {
host: getAuthHost(),
clientId
})

it('permits admin alice to do a transfer', async () => {
const { authClient } = await buildAuthClient(alicePrivateKey, {
host: getAuthHost(),
clientId
const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})

const response = await authClient.requestAccessToken(genNonce(request))
expect(response).toMatchObject({ value: expect.any(String) })
})
})
Loading

0 comments on commit 588fc61

Please sign in to comment.