-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add checking issuer for awarding badge
- Loading branch information
Showing
5 changed files
with
204 additions
and
95 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import type { User } from 'rumors-db/schema/users'; | ||
import type { Badge } from 'rumors-db/schema/badges'; | ||
|
||
export default { | ||
'/users/doc/user-to-award-badge': { | ||
|
@@ -15,11 +16,23 @@ export default { | |
badges: [ | ||
{ | ||
badgeId: 'test-certification-001', | ||
badgeMetaData: '{"from":"some-orgnization}', | ||
badgeMetaData: '{"from":"some-orgnization"}', | ||
isDisplayed: false, | ||
createdAt: '2020-01-01T00:00:00.000Z', | ||
updatedAt: '2020-01-01T00:00:00.000Z', | ||
}, | ||
], | ||
} satisfies User, | ||
|
||
'/badges/doc/test-certification-001': { | ||
name: 'Test Certification', | ||
displayName: 'Test Certification', | ||
description: 'A test certification badge', | ||
link: 'https://badge.source.com', | ||
icon: 'https://badge.source.com/icon.png', | ||
borderImage: 'https://badge.source.com/border.png', | ||
issuers: ['[email protected]', 'service-token-123'], | ||
createdAt: '2020-01-01T00:00:00.000Z', | ||
updatedAt: '2020-01-01T00:00:00.000Z', | ||
} satisfies Badge, | ||
}; |
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 |
---|---|---|
|
@@ -7,68 +7,104 @@ import fixtures from '../__fixtures__/awardBadge'; | |
|
||
const FIXED_DATE = 612921600000; | ||
|
||
beforeEach(() => loadFixtures(fixtures)); | ||
afterEach(() => unloadFixtures(fixtures)); | ||
|
||
it('fails if userId is not valid', async () => { | ||
await expect( | ||
awardBadge({ | ||
userId: 'not-exist', | ||
badgeId: 'badge id', | ||
badgeMetaData: '{}', | ||
}) | ||
).rejects.toMatchInlineSnapshot( | ||
`[HTTPError: User with ID=not-exist does not exist]` | ||
); | ||
beforeEach(async () => { | ||
await loadFixtures(fixtures); | ||
MockDate.set(FIXED_DATE); | ||
}); | ||
|
||
/** | ||
* Asserts the document in database is the same as in the fixture, | ||
* i.e. the document is not modified | ||
* | ||
* @param {string} fixtureKey | ||
* @param {{index: string; id: string;}} clientGetArgs - Arguments for client.get() | ||
*/ | ||
afterEach(async () => { | ||
await unloadFixtures(fixtures); | ||
MockDate.reset(); | ||
}); | ||
|
||
it('correctly sets the awarded badge id and updates status of their works', async () => { | ||
MockDate.set(FIXED_DATE); | ||
const result = await awardBadge({ | ||
userId: 'user-to-award-badge', | ||
badgeId: 'test-certification-001', | ||
badgeMetaData: '{"from":"some-orgnization}', | ||
describe('awardBadge', () => { | ||
it('fails if userId is not valid', async () => { | ||
await expect( | ||
awardBadge({ | ||
userId: 'not-exist', | ||
badgeId: 'badge id', | ||
badgeMetaData: '{}', | ||
request: { userId: '[email protected]' }, | ||
}) | ||
).rejects.toMatchInlineSnapshot( | ||
`[HTTPError: User with ID=not-exist does not exist]` | ||
); | ||
}); | ||
MockDate.reset(); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"badgeId": "test-certification-001", | ||
"badgeMetaData": "{\\"from\\":\\"some-orgnization}", | ||
} | ||
`); | ||
/** | ||
* Asserts the document in database is the same as in the fixture, | ||
* i.e. the document is not modified | ||
* | ||
* @param {string} fixtureKey | ||
* @param {{index: string; id: string;}} clientGetArgs - Arguments for client.get() | ||
*/ | ||
|
||
it('correctly sets the awarded badge id when authorized', async () => { | ||
const result = await awardBadge({ | ||
userId: 'user-to-award-badge', | ||
badgeId: 'test-certification-001', | ||
badgeMetaData: '{"from":"some-orgnization"}', | ||
request: { userId: '[email protected]' }, | ||
}); | ||
|
||
const { | ||
body: { _source: userWithBadge }, | ||
} = await client.get({ | ||
index: 'users', | ||
type: 'doc', | ||
id: 'user-to-award-badge', | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"badgeId": "test-certification-001", | ||
"badgeMetaData": "{\\"from\\":\\"some-orgnization\\"}", | ||
} | ||
`); | ||
|
||
const { | ||
body: { _source: userWithBadge }, | ||
} = await client.get({ | ||
index: 'users', | ||
type: 'doc', | ||
id: 'user-to-award-badge', | ||
}); | ||
|
||
// Assert that badgeId is written on the user | ||
expect(userWithBadge).toMatchInlineSnapshot(` | ||
Object { | ||
"badges": Array [ | ||
Object { | ||
"badgeId": "test-certification-001", | ||
"badgeMetaData": "{\\"from\\":\\"some-orgnization\\"}", | ||
"createdAt": "1989-06-04T00:00:00.000Z", | ||
"isDisplayed": true, | ||
"updatedAt": "1989-06-04T00:00:00.000Z", | ||
}, | ||
], | ||
"createdAt": "2020-01-01T00:00:00.000Z", | ||
"googleId": "some-google-id", | ||
"name": "user-to-award-badge", | ||
} | ||
`); | ||
}); | ||
|
||
// Assert that badgeId is written on the user | ||
expect(userWithBadge).toMatchInlineSnapshot(` | ||
Object { | ||
"badges": Array [ | ||
Object { | ||
"badgeId": "test-certification-001", | ||
"badgeMetaData": "{\\"from\\":\\"some-orgnization}", | ||
"createdAt": "1989-06-04T00:00:00.000Z", | ||
"isDisplayed": true, | ||
"updatedAt": "1989-06-04T00:00:00.000Z", | ||
}, | ||
], | ||
"createdAt": "2020-01-01T00:00:00.000Z", | ||
"googleId": "some-google-id", | ||
"name": "user-to-award-badge", | ||
} | ||
`); | ||
it('allows service token to award badge', async () => { | ||
const result = await awardBadge({ | ||
userId: 'user-to-award-badge', | ||
badgeId: 'test-certification-001', | ||
badgeMetaData: '{"from":"service"}', | ||
request: { userId: 'service-token-123' }, | ||
}); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"badgeId": "test-certification-001", | ||
"badgeMetaData": "{\\"from\\":\\"service\\"}", | ||
} | ||
`); | ||
|
||
const { | ||
body: { _source: userWithBadge }, | ||
} = await client.get({ | ||
index: 'users', | ||
type: 'doc', | ||
id: 'user-to-award-badge', | ||
}); | ||
|
||
expect(userWithBadge.badges).toHaveLength(1); | ||
expect(userWithBadge.badges[0].badgeId).toBe('test-certification-001'); | ||
}); | ||
}); |
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