-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding endpoint to retrieve user metadata by user (#5471)
- Loading branch information
Showing
3 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
locksmith/__tests__/controllers/metadataController/readAddressHolderMetadata.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import request from 'supertest' | ||
import * as sigUtil from 'eth-sig-util' | ||
import * as ethJsUtil from 'ethereumjs-util' | ||
import { keyTypedData } from '../../test-helpers/typeDataGenerators' | ||
import { addMetadata } from '../../../src/operations/userMetadataOperations' | ||
|
||
import app = require('../../../src/app') | ||
import Base64 = require('../../../src/utils/base64') | ||
|
||
const keyHolder = [ | ||
'0xAaAdEED4c0B861cB36f4cE006a9C90BA2E43fdc2', | ||
'0x6f7a54d6629b7416e17fc472b4003ae8ef18ef4c', | ||
] | ||
const lockAddress = '0x95de5F777A3e283bFf0c47374998E10D8A2183C7' | ||
const privateKey = ethJsUtil.toBuffer( | ||
'0xfd8abdd241b9e7679e3ef88f05b31545816d6fbcaf11e86ebd5a57ba281ce229' | ||
) | ||
|
||
const mockOnChainLockOwnership = { | ||
owner: jest.fn(() => { | ||
return Promise.resolve(keyHolder[0]) | ||
}), | ||
} | ||
|
||
const mockKeyHoldersByLock = { | ||
getKeyHoldingAddresses: jest.fn(() => { | ||
return Promise.resolve([keyHolder[0]]) | ||
}), | ||
} | ||
|
||
jest.mock('../../../src/utils/lockData', () => { | ||
return function() { | ||
return mockOnChainLockOwnership | ||
} | ||
}) | ||
|
||
jest.mock('../../../src/graphql/datasource/keyholdersByLock', () => ({ | ||
__esModule: true, | ||
KeyHoldersByLock: jest.fn(() => { | ||
return mockKeyHoldersByLock | ||
}), | ||
})) | ||
|
||
describe('reading address holder metadata', () => { | ||
beforeAll(async () => { | ||
await addMetadata({ | ||
tokenAddress: lockAddress, | ||
userAddress: keyHolder[0], | ||
data: { | ||
protected: { | ||
hidden: 'metadata', | ||
}, | ||
public: { | ||
mock: 'values', | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
it('yields the stored passed data if the timestamp is recent', async () => { | ||
expect.assertions(2) | ||
const typedData = keyTypedData({ | ||
UserMetaData: { | ||
owner: keyHolder[0], | ||
timestamp: Date.now(), | ||
}, | ||
}) | ||
|
||
const sig = sigUtil.signTypedData(privateKey, { | ||
data: typedData, | ||
}) | ||
|
||
const response = await request(app) | ||
.get(`/api/key/${lockAddress}/user/${keyHolder[0]}`) | ||
.set('Accept', 'json') | ||
.set('Authorization', `Bearer ${Base64.encode(sig)}`) | ||
.query({ data: encodeURIComponent(JSON.stringify(typedData)) }) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.body).toEqual({ | ||
userMetadata: { | ||
protected: { | ||
hidden: 'metadata', | ||
}, | ||
public: { | ||
mock: 'values', | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
it('does not yield the stored passed data if the timestamp is old', async () => { | ||
expect.assertions(2) | ||
const typedData = keyTypedData({ | ||
UserMetaData: { | ||
owner: keyHolder[0], | ||
timestamp: 0, | ||
}, | ||
}) | ||
|
||
const sig = sigUtil.signTypedData(privateKey, { | ||
data: typedData, | ||
}) | ||
|
||
const response = await request(app) | ||
.get(`/api/key/${lockAddress}/user/${keyHolder[0]}`) | ||
.set('Accept', 'json') | ||
.set('Authorization', `Bearer ${Base64.encode(sig)}`) | ||
.query({ data: encodeURIComponent(JSON.stringify(typedData)) }) | ||
|
||
expect(response.status).toEqual(401) | ||
expect(response.body).toEqual({}) | ||
}) | ||
|
||
describe('when an invalid signature is passed', () => { | ||
it('returns unauthorized', async () => { | ||
expect.assertions(2) | ||
|
||
const typedData = keyTypedData({ | ||
UserMetaData: { | ||
owner: keyHolder[0], | ||
protected: { | ||
hidden: 'metadata', | ||
}, | ||
public: { | ||
mock: 'values', | ||
}, | ||
}, | ||
}) | ||
|
||
const sig = sigUtil.signTypedData(privateKey, { | ||
data: typedData, | ||
}) | ||
|
||
const response = await request(app) | ||
.get(`/api/key/${lockAddress}/user/${keyHolder[0]}`) | ||
.set('Accept', 'json') | ||
.set('Authorization', `Bearer ${Base64.encode(sig)}`) | ||
.query({ data: encodeURIComponent(JSON.stringify(typedData)) }) | ||
|
||
expect(response.status).toEqual(401) | ||
expect(response.body).toEqual({}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters