@verdaccio/api 6.0.0-6-next.30 → 6.0.0-6-next.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,264 +1,169 @@
1
- import _ from 'lodash';
2
1
  import supertest from 'supertest';
3
2
 
4
- import {
5
- API_ERROR,
6
- API_MESSAGE,
7
- HEADERS,
8
- HEADER_TYPE,
9
- HTTP_STATUS,
10
- errorUtils,
11
- } from '@verdaccio/core';
3
+ import { API_ERROR, HEADERS, HEADER_TYPE, HTTP_STATUS, TOKEN_BEARER } from '@verdaccio/core';
4
+ import { buildToken } from '@verdaccio/utils';
12
5
 
13
- import { $RequestExtend, $ResponseExtend } from '../../types/custom';
14
- import { initializeServer } from './_helper';
6
+ import { createUser, getPackage, initializeServer } from './_helper';
15
7
 
16
- const mockApiJWTmiddleware = jest.fn(
17
- () =>
18
- (req: $RequestExtend, res: $ResponseExtend, _next): void => {
19
- req.remote_user = { name: 'test', groups: [], real_groups: [] };
20
- _next();
21
- }
22
- );
8
+ const FORBIDDEN_VUE = 'authorization required to access package vue';
23
9
 
24
- const mockAuthenticate = jest.fn(() => (_name, _password, callback): void => {
25
- return callback(null, ['all']);
26
- });
10
+ jest.setTimeout(20000);
27
11
 
28
- const mockAddUser = jest.fn(() => (_name, _password, callback): void => {
29
- return callback(errorUtils.getConflict(API_ERROR.USERNAME_ALREADY_REGISTERED));
30
- });
12
+ describe('token', () => {
13
+ describe('basics', () => {
14
+ const FAKE_TOKEN: string = buildToken(TOKEN_BEARER, 'fake');
15
+ test.each([['user.yaml'], ['user.jwt.yaml']])('should test add a new user', async (conf) => {
16
+ const app = await initializeServer(conf);
17
+ const credentials = { name: 'JotaJWT', password: 'secretPass' };
18
+ const response = await createUser(app, credentials.name, credentials.password);
19
+ expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
31
20
 
32
- jest.mock('@verdaccio/auth', () => ({
33
- getApiToken: () => 'token',
34
- Auth: class {
35
- apiJWTmiddleware() {
36
- return mockApiJWTmiddleware();
37
- }
38
- allow_access(_d, f_, cb) {
39
- cb(null, true);
40
- }
41
- add_user(name, password, callback) {
42
- mockAddUser()(name, password, callback);
43
- }
44
- authenticate(_name, _password, callback) {
45
- mockAuthenticate()(_name, _password, callback);
46
- }
47
- },
48
- }));
21
+ const vueResponse = await getPackage(app, response.body.token, 'vue');
22
+ expect(vueResponse.body).toBeDefined();
23
+ expect(vueResponse.body.name).toMatch('vue');
49
24
 
50
- // FIXME: This might be covered with user.jwt.spec
51
- describe('user', () => {
52
- const credentials = { name: 'test', password: 'test' };
25
+ const vueFailResp = await getPackage(app, FAKE_TOKEN, 'vue', HTTP_STATUS.UNAUTHORIZED);
26
+ expect(vueFailResp.body.error).toMatch(FORBIDDEN_VUE);
27
+ });
53
28
 
54
- test('should test add a new user', async () => {
55
- mockApiJWTmiddleware.mockImplementationOnce(
56
- () =>
57
- (req: $RequestExtend, res: $ResponseExtend, _next): void => {
58
- req.remote_user = { name: undefined };
59
- _next();
60
- }
61
- );
29
+ test.each([['user.yaml'], ['user.jwt.yaml']])('should login an user', async (conf) => {
30
+ const app = await initializeServer(conf);
31
+ const credentials = { name: 'test', password: 'test' };
32
+ const response = await createUser(app, credentials.name, credentials.password);
33
+ expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
62
34
 
63
- mockAddUser.mockImplementationOnce(() => (_name, _password, callback): void => {
64
- return callback(null, true);
65
- });
66
- const app = await initializeServer('user.yaml');
67
- return new Promise((resolve, reject) => {
68
- supertest(app)
69
- .put(`/-/user/org.couchdb.user:newUser`)
35
+ await supertest(app)
36
+ .put(`/-/user/org.couchdb.user:${credentials.name}`)
70
37
  .send({
71
- name: 'newUser',
72
- password: 'newUser',
38
+ name: credentials.name,
39
+ password: credentials.password,
73
40
  })
41
+ .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, response.body.token))
74
42
  .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
75
- .expect(HTTP_STATUS.CREATED)
76
- .end(function (err, res) {
77
- if (err) {
78
- return reject(err);
79
- }
80
- expect(res.body.ok).toBeDefined();
81
- expect(res.body.token).toBeDefined();
82
- const token = res.body.token;
83
- expect(typeof token).toBe('string');
84
- expect(res.body.ok).toMatch(`user 'newUser' created`);
85
- resolve(null);
86
- });
43
+ .expect(HTTP_STATUS.CREATED);
87
44
  });
88
- });
89
45
 
90
- test('should test fails on add a existing user with login', async () => {
91
- mockApiJWTmiddleware.mockImplementationOnce(
92
- () =>
93
- (req: $RequestExtend, res: $ResponseExtend, _next): void => {
94
- req.remote_user = { name: undefined };
95
- _next();
96
- }
46
+ test.each([['user.yaml'], ['user.jwt.yaml']])(
47
+ 'should fails login a valid user',
48
+ async (conf) => {
49
+ const app = await initializeServer(conf);
50
+ const credentials = { name: 'test', password: 'test' };
51
+ const response = await createUser(app, credentials.name, credentials.password);
52
+ expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
53
+
54
+ await supertest(app)
55
+ .put(`/-/user/org.couchdb.user:${credentials.name}`)
56
+ .send({
57
+ name: credentials.name,
58
+ password: 'failPassword',
59
+ })
60
+ .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, response.body.token))
61
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
62
+ .expect(HTTP_STATUS.UNAUTHORIZED);
63
+ }
97
64
  );
98
- const app = await initializeServer('user.yaml');
99
- return new Promise((resolve, reject) => {
100
- supertest(app)
101
- .put('/-/user/org.couchdb.user:jotaNew')
102
- .send(credentials)
103
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
104
- .expect(HTTP_STATUS.CONFLICT)
105
- .end(function (err, res) {
106
- if (err) {
107
- return reject(err);
108
- }
109
- expect(res.body.error).toBeDefined();
110
- expect(res.body.error).toMatch(API_ERROR.USERNAME_ALREADY_REGISTERED);
111
- resolve(res.body);
112
- });
113
- });
114
- });
115
65
 
116
- test('should log in as existing user', async () => {
117
- const app = await initializeServer('user.yaml');
118
- return new Promise((resolve, reject) => {
119
- supertest(app)
120
- .put(`/-/user/org.couchdb.user:${credentials.name}`)
121
- .send(credentials)
122
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
123
- .expect(HTTP_STATUS.CREATED)
124
- .end((err, res) => {
125
- if (err) {
126
- return reject(err);
127
- }
128
-
129
- expect(res.body).toBeTruthy();
130
- expect(res.body.ok).toMatch(`you are authenticated as \'${credentials.name}\'`);
131
- resolve(res);
132
- });
133
- });
134
- });
135
-
136
- test('should test fails add a new user with missing name', async () => {
137
- mockApiJWTmiddleware.mockImplementationOnce(
138
- () =>
139
- (req: $RequestExtend, res: $ResponseExtend, _next): void => {
140
- req.remote_user = { name: undefined };
141
- _next();
142
- }
66
+ test.each([['user.yaml'], ['user.jwt.yaml']])(
67
+ 'should test conflict create new user',
68
+ async (conf) => {
69
+ const app = await initializeServer(conf);
70
+ const credentials = { name: 'JotaJWT', password: 'secretPass' };
71
+ const response = await createUser(app, credentials.name, credentials.password);
72
+ expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
73
+ const response2 = await supertest(app)
74
+ .put(`/-/user/org.couchdb.user:${credentials.name}`)
75
+ .send({
76
+ name: credentials.name,
77
+ password: credentials.password,
78
+ })
79
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
80
+ .expect(HTTP_STATUS.CONFLICT);
81
+ expect(response2.body.error).toBe(API_ERROR.USERNAME_ALREADY_REGISTERED);
82
+ }
143
83
  );
144
- mockAddUser.mockImplementationOnce(() => (_name, _password, callback): void => {
145
- return callback(errorUtils.getBadRequest(API_ERROR.USERNAME_PASSWORD_REQUIRED));
146
- });
147
- const credentialsShort = _.cloneDeep(credentials);
148
- delete credentialsShort.name;
149
84
 
150
- const app = await initializeServer('user.yaml');
151
- return new Promise((resolve, reject) => {
152
- supertest(app)
153
- .put(`/-/user/org.couchdb.user:${credentials.name}`)
154
- .send(credentialsShort)
155
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
156
- .expect(HTTP_STATUS.BAD_REQUEST)
157
- .end(function (err, res) {
158
- if (err) {
159
- return reject(err);
160
- }
161
-
162
- expect(res.body.error).toBeDefined();
163
- expect(res.body.error).toMatch(API_ERROR.USERNAME_PASSWORD_REQUIRED);
164
- resolve(app);
165
- });
166
- });
167
- });
168
-
169
- test('should test fails add a new user with missing password', async () => {
170
- mockApiJWTmiddleware.mockImplementationOnce(
171
- () =>
172
- (req: $RequestExtend, res: $ResponseExtend, _next): void => {
173
- req.remote_user = { name: undefined };
174
- _next();
175
- }
85
+ test.each([['user.yaml'], ['user.jwt.yaml']])(
86
+ 'should fails on login if user credentials are invalid',
87
+ async (conf) => {
88
+ const app = await initializeServer(conf);
89
+ const credentials = { name: 'newFailsUser', password: 'secretPass' };
90
+ const response = await createUser(app, credentials.name, credentials.password);
91
+ expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
92
+ const response2 = await supertest(app)
93
+ .put(`/-/user/org.couchdb.user:${credentials.name}`)
94
+ .send({
95
+ name: credentials.name,
96
+ password: 'BAD_PASSWORD',
97
+ })
98
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
99
+ .expect(HTTP_STATUS.UNAUTHORIZED);
100
+ expect(response2.body.error).toBe(API_ERROR.UNAUTHORIZED_ACCESS);
101
+ }
176
102
  );
177
- const credentialsShort = _.cloneDeep(credentials);
178
- delete credentialsShort.password;
179
-
180
- const app = await initializeServer('user.yaml');
181
- return new Promise((resolve, reject) => {
182
- supertest(app)
183
- .put(`/-/user/org.couchdb.user:${credentials.name}`)
184
- .send(credentialsShort)
185
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
186
- .expect(HTTP_STATUS.BAD_REQUEST)
187
- .end(function (err, res) {
188
- if (err) {
189
- return reject(err);
190
- }
191
-
192
- expect(res.body.error).toBeDefined();
193
- // FIXME: message is not 100% accurate
194
- // eslint-disable-next-line new-cap
195
- expect(res.body.error).toMatch(API_ERROR.PASSWORD_SHORT());
196
- resolve(res);
197
- });
198
- });
199
- });
200
103
 
201
- test('should test fails add a new user with wrong password', async () => {
202
- mockApiJWTmiddleware.mockImplementationOnce(
203
- () =>
204
- (req: $RequestExtend, res: $ResponseExtend, _next): void => {
205
- req.remote_user = { name: 'test' };
206
- _next();
207
- }
104
+ test.each([['user.yaml'], ['user.jwt.yaml']])(
105
+ 'should fails password validation',
106
+ async (conf) => {
107
+ const credentials = { name: 'test', password: '12' };
108
+ const app = await initializeServer(conf);
109
+ const response = await supertest(app)
110
+ .put(`/-/user/org.couchdb.user:${credentials.name}`)
111
+ .send({
112
+ name: credentials.name,
113
+ password: credentials.password,
114
+ })
115
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
116
+ .expect(HTTP_STATUS.BAD_REQUEST);
117
+ expect(response.body.error).toBe(API_ERROR.PASSWORD_SHORT);
118
+ }
208
119
  );
209
- mockAuthenticate.mockImplementationOnce(() => (_name, _password, callback): void => {
210
- return callback(errorUtils.getUnauthorized(API_ERROR.BAD_USERNAME_PASSWORD));
211
- });
212
- const credentialsShort = _.cloneDeep(credentials);
213
- credentialsShort.password = 'failPassword';
214
- const app = await initializeServer('user.yaml');
215
- return new Promise((resolve, reject) => {
216
- supertest(app)
217
- .put('/-/user/org.couchdb.user:test')
218
- .send(credentialsShort)
219
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
220
- .expect(HTTP_STATUS.UNAUTHORIZED)
221
- .end(function (err, res) {
222
- if (err) {
223
- return reject(err);
224
- }
225
120
 
226
- expect(res.body.error).toBeDefined();
227
- expect(res.body.error).toMatch(API_ERROR.BAD_USERNAME_PASSWORD);
228
- resolve(res);
229
- });
230
- });
231
- });
121
+ test.each([['user.yaml'], ['user.jwt.yaml']])(
122
+ 'should fails missing password validation',
123
+ async (conf) => {
124
+ const credentials = { name: 'test' };
125
+ const app = await initializeServer(conf);
126
+ const response = await supertest(app)
127
+ .put(`/-/user/org.couchdb.user:${credentials.name}`)
128
+ .send({
129
+ name: credentials.name,
130
+ password: undefined,
131
+ })
132
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
133
+ .expect(HTTP_STATUS.BAD_REQUEST);
134
+ expect(response.body.error).toBe(API_ERROR.PASSWORD_SHORT);
135
+ }
136
+ );
232
137
 
233
- test('should be able to logout an user', async () => {
234
- mockApiJWTmiddleware.mockImplementationOnce(
235
- () =>
236
- (req: $RequestExtend, _res: $ResponseExtend, _next): void => {
237
- req.remote_user = { name: 'test' };
238
- _next();
239
- }
138
+ test.each([['user.yaml'], ['user.jwt.yaml']])(
139
+ 'should verify if user is logged',
140
+ async (conf) => {
141
+ const app = await initializeServer(conf);
142
+ const credentials = { name: 'jota', password: 'secretPass' };
143
+ const response = await createUser(app, credentials.name, credentials.password);
144
+ expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
145
+ const response2 = await supertest(app)
146
+ .get(`/-/user/org.couchdb.user:${credentials.name}`)
147
+ .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, response.body.token))
148
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
149
+ .expect(HTTP_STATUS.OK);
150
+ expect(response2.body.ok).toBe(`you are authenticated as '${credentials.name}'`);
151
+ }
240
152
  );
241
- mockAuthenticate.mockImplementationOnce(() => (_name, _password, callback): void => {
242
- return callback(errorUtils.getUnauthorized(API_ERROR.BAD_USERNAME_PASSWORD));
243
- });
244
- const credentialsShort = _.cloneDeep(credentials);
245
- credentialsShort.password = 'failPassword';
246
153
 
247
- const app = await initializeServer('user.yaml');
248
- return new Promise((resolve, reject) => {
249
- supertest(app)
250
- .delete('/-/user/token/someSecretToken')
251
- .send(credentialsShort)
154
+ test.each([['user.yaml'], ['user.jwt.yaml']])('should logout user', async (conf) => {
155
+ const app = await initializeServer(conf);
156
+ const credentials = { name: 'jota', password: 'secretPass' };
157
+ const response = await createUser(app, credentials.name, credentials.password);
158
+ await supertest(app)
159
+ .get(`/-/user/org.couchdb.user:${credentials.name}`)
160
+ .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, response.body.token))
252
161
  .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
253
- .expect(HTTP_STATUS.OK)
254
- .end(function (err, res) {
255
- if (err) {
256
- return reject(err);
257
- }
258
-
259
- expect(res.body.ok).toMatch(API_MESSAGE.LOGGED_OUT);
260
- resolve(res);
261
- });
162
+ .expect(HTTP_STATUS.OK);
163
+ await supertest(app)
164
+ .delete(`/-/user/token/someSecretToken:${response.body.token}`)
165
+ .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
166
+ .expect(HTTP_STATUS.OK);
262
167
  });
263
168
  });
264
169
  });
@@ -1,78 +0,0 @@
1
- import supertest from 'supertest';
2
-
3
- import { API_ERROR, HEADERS, HEADER_TYPE, HTTP_STATUS, TOKEN_BEARER } from '@verdaccio/core';
4
- import { buildToken } from '@verdaccio/utils';
5
-
6
- import { createUser, getPackage, initializeServer } from './_helper';
7
-
8
- const FORBIDDEN_VUE = 'authorization required to access package vue';
9
-
10
- describe('token', () => {
11
- describe('basics', () => {
12
- const FAKE_TOKEN: string = buildToken(TOKEN_BEARER, 'fake');
13
- test.each([['user.yaml'], ['user.jwt.yaml']])('should test add a new user', async (conf) => {
14
- const app = await initializeServer(conf);
15
- const credentials = { name: 'JotaJWT', password: 'secretPass' };
16
- const response = await createUser(app, credentials.name, credentials.password);
17
- expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
18
-
19
- const vueResponse = await getPackage(app, response.body.token, 'vue');
20
- expect(vueResponse.body).toBeDefined();
21
- expect(vueResponse.body.name).toMatch('vue');
22
-
23
- const vueFailResp = await getPackage(app, FAKE_TOKEN, 'vue', HTTP_STATUS.UNAUTHORIZED);
24
- expect(vueFailResp.body.error).toMatch(FORBIDDEN_VUE);
25
- });
26
-
27
- test.each([['user.yaml'], ['user.jwt.yaml']])('should test add a new user', async (conf) => {
28
- const app = await initializeServer(conf);
29
- const credentials = { name: 'JotaJWT', password: 'secretPass' };
30
- const response = await createUser(app, credentials.name, credentials.password);
31
- expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
32
- const response2 = await supertest(app)
33
- .put(`/-/user/org.couchdb.user:${credentials.name}`)
34
- .send({
35
- name: credentials.name,
36
- password: credentials.password,
37
- })
38
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
39
- .expect(HTTP_STATUS.CONFLICT);
40
- expect(response2.body.error).toBe(API_ERROR.USERNAME_ALREADY_REGISTERED);
41
- });
42
-
43
- test.each([['user.yaml'], ['user.jwt.yaml']])(
44
- 'should fails on login if user credentials are invalid even if jwt',
45
- async (conf) => {
46
- const app = await initializeServer(conf);
47
- const credentials = { name: 'newFailsUser', password: 'secretPass' };
48
- const response = await createUser(app, credentials.name, credentials.password);
49
- expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
50
- const response2 = await supertest(app)
51
- .put(`/-/user/org.couchdb.user:${credentials.name}`)
52
- .send({
53
- name: credentials.name,
54
- password: 'BAD_PASSWORD',
55
- })
56
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
57
- .expect(HTTP_STATUS.UNAUTHORIZED);
58
- expect(response2.body.error).toBe(API_ERROR.UNAUTHORIZED_ACCESS);
59
- }
60
- );
61
-
62
- test.each([['user.yaml'], ['user.jwt.yaml']])(
63
- 'should verify if user is logged',
64
- async (conf) => {
65
- const app = await initializeServer(conf);
66
- const credentials = { name: 'jota', password: 'secretPass' };
67
- const response = await createUser(app, credentials.name, credentials.password);
68
- expect(response.body.ok).toMatch(`user '${credentials.name}' created`);
69
- const response2 = await supertest(app)
70
- .get(`/-/user/org.couchdb.user:${credentials.name}`)
71
- .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, response.body.token))
72
- .expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
73
- .expect(HTTP_STATUS.OK);
74
- expect(response2.body.ok).toBe(`you are authenticated as '${credentials.name}'`);
75
- }
76
- );
77
- });
78
- });