|
| 1 | +import sinon from 'sinon'; |
| 2 | +import { Users } from '.'; |
| 3 | +import { headersWithToken } from '../../shared/headers'; |
| 4 | +import { HttpClient } from '../../shared/http/client'; |
| 5 | +import { UserPublicKeyWithCreationResponse } from './types'; |
| 6 | + |
| 7 | +const httpClient = HttpClient.create(''); |
| 8 | + |
| 9 | +describe('Users service tests', () => { |
| 10 | + beforeEach(() => { |
| 11 | + sinon.stub(HttpClient, 'create').returns(httpClient); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + sinon.restore(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('Users methods', () => { |
| 19 | + describe('getPublicKeyWithPrecreation', () => { |
| 20 | + it('should call the correct endpoint and return the public key response', async () => { |
| 21 | + const email = 'test@example.com'; |
| 22 | + const publicKeyResponse: UserPublicKeyWithCreationResponse = { |
| 23 | + publicKey: 'public_key_example_123', |
| 24 | + publicKyberKey: 'kyber_key_example_123', |
| 25 | + }; |
| 26 | + |
| 27 | + const { client, headers } = clientAndHeaders(); |
| 28 | + const putCall = sinon.stub(httpClient, 'put').resolves(publicKeyResponse); |
| 29 | + |
| 30 | + const response = await client.getPublicKeyWithPrecreation({ email }); |
| 31 | + |
| 32 | + expect(putCall.firstCall.args).toEqual([`/users/public-key/${email}`, {}, headers]); |
| 33 | + expect(response).toEqual(publicKeyResponse); |
| 34 | + }); |
| 35 | + }); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +function clientAndHeaders( |
| 40 | + apiUrl = '', |
| 41 | + clientName = 'c-name', |
| 42 | + clientVersion = '0.1', |
| 43 | + token = 'my-token', |
| 44 | +): { |
| 45 | + client: Users; |
| 46 | + headers: object; |
| 47 | +} { |
| 48 | + const appDetails = { |
| 49 | + clientName, |
| 50 | + clientVersion, |
| 51 | + }; |
| 52 | + const apiSecurity = { |
| 53 | + token, |
| 54 | + unauthorizedCallback: () => {}, |
| 55 | + }; |
| 56 | + const client = Users.client(apiUrl, appDetails, apiSecurity); |
| 57 | + const headers = headersWithToken(clientName, clientVersion, token); |
| 58 | + return { client, headers }; |
| 59 | +} |
0 commit comments