-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.test.ts
More file actions
206 lines (184 loc) · 5.49 KB
/
Copy pathindex.test.ts
File metadata and controls
206 lines (184 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import sinon from 'sinon';
import { Users } from '../../../src/drive';
import { ChangePasswordPayloadNew } from '../../../src/drive/users/types';
import { ApiSecurity, AppDetails } from '../../../src/shared';
import { headersWithToken } from '../../../src/shared/headers';
import { HttpClient } from '../../../src/shared/http/client';
const httpClient = HttpClient.create('');
describe('# users service tests', () => {
beforeEach(() => {
sinon.stub(HttpClient, 'create').returns(httpClient);
});
afterEach(() => {
sinon.restore();
});
describe('send invitation', () => {
it('should call with right params & return response', async () => {
// Arrange
const { client, headers } = clientAndHeaders();
const postStub = sinon.stub(httpClient, 'post').resolves({
sent: true,
});
const email = 'my@email.com';
// Act
const body = await client.sendInvitation(email);
// Assert
expect(postStub.firstCall.args).toEqual([
'/user/invite',
{
email: email,
},
headers,
]);
expect(body).toEqual({
sent: true,
});
});
});
describe('initialize', () => {
it('should call with right params & return response', async () => {
// Arrange
const { client, headers } = clientAndHeaders();
const callStub = sinon.stub(httpClient, 'post').resolves({
user: {
root_folder: 1,
},
});
const email = 'e',
mnemonic = 'm';
// Act
const body = await client.initialize(email, mnemonic);
// Assert
expect(callStub.firstCall.args).toEqual([
'/initialize',
{
email: email,
mnemonic: mnemonic,
},
headers,
]);
expect(body).toEqual({
root_folder: 1,
});
});
});
describe('refresh', () => {
it('should call with right params & return response', async () => {
// Arrange
const { client, headers } = clientAndHeaders();
const callStub = sinon.stub(httpClient, 'get').resolves({
user: {},
token: 't',
});
// Act
const body = await client.refreshUser();
// Assert
expect(callStub.firstCall.args).toEqual(['/user/refresh', headers]);
expect(body).toEqual({
user: {},
token: 't',
});
});
});
describe('refresh user avatar', () => {
it('should call with right params & return response', async () => {
// Arrange
const { client, headers } = clientAndHeaders();
const mockedAvatarUrl = 'https://example.avatar.com/avatar.jpg';
const callStub = sinon.stub(httpClient, 'get').resolves({
avatar: mockedAvatarUrl,
});
// Act
const body = await client.refreshAvatarUser();
// Assert
expect(callStub.firstCall.args).toStrictEqual(['/users/avatar/refresh', headers]);
expect(body).toStrictEqual({
avatar: mockedAvatarUrl,
});
});
});
describe('pre register user', () => {
it('should pre create user', async () => {
// Arrange
const { client, headers } = clientAndHeaders();
const email = 'test@test.com';
const callStub = sinon.stub(httpClient, 'post').resolves({
publicKey: 'publicKey',
keys: {
kyber: 'publicKeyberKey',
ecc: 'publicKey',
},
user: { uuid: 'exampleUuid', email },
});
// Act
const body = await client.preRegister(email);
// Assert
expect(callStub.firstCall.args).toEqual(['/users/pre-create', { email }, headers]);
expect(body).toEqual({
publicKey: 'publicKey',
keys: {
kyber: 'publicKeyberKey',
ecc: 'publicKey',
},
user: { uuid: 'exampleUuid', email },
});
});
});
describe('change password', () => {
it('should call with right params & return response', async () => {
// Arrange
const { client, headers } = clientAndHeaders();
const callStub = sinon.stub(httpClient, 'patch').resolves({});
const payload: ChangePasswordPayloadNew = {
currentEncryptedPassword: '1',
encryptedMnemonic: '2',
encryptedPrivateKey: '3',
keys: {
encryptedPrivateKey: '3',
encryptedPrivateKyberKey: '4',
},
newEncryptedPassword: '5',
newEncryptedSalt: '6',
encryptVersion: '7',
};
// Act
const body = await client.changePassword(payload);
// Assert
expect(callStub.firstCall.args).toEqual([
'/users/password',
{
currentPassword: payload.currentEncryptedPassword,
newPassword: payload.newEncryptedPassword,
newSalt: payload.newEncryptedSalt,
mnemonic: payload.encryptedMnemonic,
privateKey: payload.keys.encryptedPrivateKey,
privateKyberKey: payload.keys.encryptedPrivateKyberKey,
encryptVersion: payload.encryptVersion,
},
headers,
]);
expect(body).toEqual({});
});
});
});
function clientAndHeaders(
apiUrl = '',
clientName = 'c-name',
clientVersion = '0.1',
token = 'my-token',
mnemonic = 'nemo',
): {
client: Users;
headers: object;
} {
const appDetails: AppDetails = {
clientName: clientName,
clientVersion: clientVersion,
};
const apiSecurity: ApiSecurity = {
token: token,
};
const client = Users.client(apiUrl, appDetails, apiSecurity);
const headers = headersWithToken(clientName, clientVersion, token);
return { client, headers };
}