Skip to content

Commit 37c46e6

Browse files
committed
chore: misc corrections
- refactor unit tests to avoid code duplication - fix wrong root folder to tests, shows correct code coverage of src files - add minimum node 8 to README
1 parent adcef60 commit 37c46e6

File tree

5 files changed

+64
-96
lines changed

5 files changed

+64
-96
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ the following:
2222
- If `legacy_mode: false` or undefined (default mode): Gitlab 11.2+
2323
- If `legacy_mode: true`: Gitlab 9.0+
2424

25+
## Node Compatibility
26+
27+
verdaccio-gitlab requires node 8+
28+
2529
## Use it
2630

2731
You need at least node version 8.x.x, codename **carbon**.

test/jest.config.unit.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
module.exports = {
44
name: 'verdaccio-gitlab-unit-jest',
55
verbose: true,
6+
rootDir: '..',
67
collectCoverage: true,
8+
collectCoverageFrom: [
9+
'src/*.{js,jsx}'
10+
],
711
testRegex: '(test/unit.*\\.spec|test/unit/webui/.*\\.spec)\\.js',
812
coveragePathIgnorePatterns: [
913
'node_modules',
1014
'fixtures',
11-
'<rootDir>/test',
1215
]
1316
};

test/unit/authcache.spec.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
// @flow
22

3-
import type { Logger } from '@verdaccio/types';
43
import type { UserDataGroups } from '../../src/authcache.js';
54

65
import { AuthCache, UserData } from '../../src/authcache.js';
6+
import logger from './partials/logger.js';
77

88

9-
const logger: Logger = {
10-
error: jest.fn(),
11-
info: jest.fn(),
12-
debug: jest.fn(),
13-
child: jest.fn(),
14-
warn: jest.fn(),
15-
http: jest.fn(),
16-
trace: jest.fn()
9+
const TEST_USER: string = 'myUser';
10+
const TEST_PASS: string = 'myPass';
11+
const TEST_DATA_GROUPS: UserDataGroups = {
12+
publish: ['fooGroup1', 'fooGroup2']
1713
};
14+
const TEST_USER_DATA: UserData = new UserData(TEST_USER, TEST_DATA_GROUPS);
15+
1816

1917
describe('AuthCache Unit Tests', () => {
2018
test('should create an AuthCache instance', () => {
@@ -32,32 +30,20 @@ describe('AuthCache Unit Tests', () => {
3230

3331
test('should store and find some user data', () => {
3432
const authCache: AuthCache = new AuthCache(logger);
35-
const user: string = 'fooUser';
36-
const pass: string = 'fooPass';
37-
const dataGroups: UserDataGroups = {
38-
publish: ['fooGroup1', 'fooGroup2']
39-
};
40-
const data: UserData = new UserData(user, dataGroups);
4133

42-
authCache.storeUser(user, pass, data);
43-
const returnedData: UserData = authCache.findUser(user, pass);
34+
authCache.storeUser(TEST_USER, TEST_PASS, TEST_USER_DATA);
35+
const returnedData: UserData = authCache.findUser(TEST_USER, TEST_PASS);
4436

45-
expect(returnedData).toEqual(data);
37+
expect(returnedData).toEqual(TEST_USER_DATA);
4638
});
4739

4840
test('should store and find some user data when ttl is unlimited', () => {
4941
const UNLIMITED_TTL: number = 0;
5042
const authCache: AuthCache = new AuthCache(logger, UNLIMITED_TTL);
51-
const user: string = 'fooUser';
52-
const pass: string = 'fooPass';
53-
const dataGroups: UserDataGroups = {
54-
publish: ['fooGroup1', 'fooGroup2']
55-
};
56-
const data: UserData = new UserData(user, dataGroups);
5743

58-
authCache.storeUser(user, pass, data);
59-
const returnedData: UserData = authCache.findUser(user, pass);
44+
authCache.storeUser(TEST_USER, TEST_PASS, TEST_USER_DATA);
45+
const returnedData: UserData = authCache.findUser(TEST_USER, TEST_PASS);
6046

61-
expect(returnedData).toEqual(data);
47+
expect(returnedData).toEqual(TEST_USER_DATA);
6248
});
6349
});

test/unit/gitlab.spec.js

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,69 @@ import logger from './partials/logger.js';
99

1010
// Do not remove, this mocks the gitlab library
1111
import Gitlab from 'gitlab'; // eslint-disable-line no-unused-vars
12-
const options = {
12+
13+
14+
const TEST_OPTIONS = {
1315
// $FlowFixMe
1416
config: {},
1517
logger: logger
1618
};
19+
const TEST_USER: string = 'myUser';
20+
const TEST_PASS: string = 'myPass';
21+
const TEST_REMOTE_USER: RemoteUser = {
22+
real_groups: ['myGroup', TEST_USER],
23+
groups: ['myGroup', TEST_USER],
24+
name: TEST_USER
25+
};
26+
1727

1828
describe('Gitlab Auth Plugin Unit Tests', () => {
1929
test('should create a plugin instance', () => {
20-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
30+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
2131

2232
expect(verdaccioGitlab).toBeDefined();
2333
});
2434

2535
test('should authenticate a user', done => {
26-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
27-
const user: string = 'myUser';
28-
const pass: string = 'myPass';
36+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
2937

3038
const cb: Callback = (err, data) => {
3139
expect(err).toBeFalsy();
3240
expect(data.sort()).toEqual(['myGroup', 'myUser'].sort());
3341
done();
3442
};
3543

36-
verdaccioGitlab.authenticate(user, pass, cb);
44+
verdaccioGitlab.authenticate(TEST_USER, TEST_PASS, cb);
3745
});
3846

3947
test('should fail authentication with wrong pass', done => {
40-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
41-
const user: string = 'myUser';
42-
const pass: string = 'anotherPass';
48+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
49+
const wrongPass: string = TEST_PASS + '_wrong';
4350

4451
const cb: Callback = (err, data) => {
4552
expect(err).toBeTruthy();
4653
expect(data).toBeFalsy();
4754
done();
4855
};
4956

50-
verdaccioGitlab.authenticate(user, pass, cb);
57+
verdaccioGitlab.authenticate(TEST_USER, wrongPass, cb);
5158
});
5259

53-
test('should fail authentication with not existing user', done => {
54-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
55-
const user: string = 'anotherUser';
56-
const pass: string = 'myPass';
60+
test('should fail authentication with non-existing user', done => {
61+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
62+
const wrongUser: string = TEST_USER + '_wrong';
5763

5864
const cb: Callback = (err, data) => {
5965
expect(err).toBeTruthy();
6066
expect(data).toBeFalsy();
6167
done();
6268
};
6369

64-
verdaccioGitlab.authenticate(user, pass, cb);
70+
verdaccioGitlab.authenticate(wrongUser, TEST_PASS, cb);
6571
});
6672

6773
test('should allow access to package based on user group', done => {
68-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
69-
const user: RemoteUser = {
70-
real_groups: ['myGroup', 'myUser'],
71-
groups: ['myGroup', 'myUser'],
72-
name: 'myUser'
73-
};
74+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
7475
const _package: VerdaccioGitlabPackageAccess = {
7576
name: '@myGroup/myPackage',
7677
access: ['$authenticated'],
@@ -83,18 +84,13 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
8384
done();
8485
};
8586

86-
verdaccioGitlab.allow_access(user, _package, cb);
87+
verdaccioGitlab.allow_access(TEST_REMOTE_USER, _package, cb);
8788
});
8889

8990
test('should allow access to package based on user name', done => {
90-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
91-
const user: RemoteUser = {
92-
real_groups: ['myGroup', 'myUser'],
93-
groups: ['myGroup', 'myUser'],
94-
name: 'myUser'
95-
};
91+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
9692
const _package: VerdaccioGitlabPackageAccess = {
97-
name: 'myUser',
93+
name: TEST_USER,
9894
access: ['$authenticated'],
9995
gitlab: true
10096
};
@@ -105,12 +101,12 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
105101
done();
106102
};
107103

108-
verdaccioGitlab.allow_access(user, _package, cb);
104+
verdaccioGitlab.allow_access(TEST_REMOTE_USER, _package, cb);
109105
});
110106

111107
test('should deny access to package based on unauthenticated', done => {
112-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
113-
const user: RemoteUser = {
108+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
109+
const unauthenticatedUser: RemoteUser = {
114110
real_groups: [],
115111
groups: [],
116112
name: undefined
@@ -127,16 +123,11 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
127123
done();
128124
};
129125

130-
verdaccioGitlab.allow_access(user, _package, cb);
126+
verdaccioGitlab.allow_access(unauthenticatedUser, _package, cb);
131127
});
132128

133129
test('should allow publish of package based on user group', done => {
134-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
135-
const user: RemoteUser = {
136-
real_groups: ['myGroup', 'myUser'],
137-
groups: ['myGroup', 'myUser'],
138-
name: 'myUser'
139-
};
130+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
140131
const _package: VerdaccioGitlabPackageAccess = {
141132
name: '@myGroup/myPackage',
142133
gitlab: true
@@ -148,18 +139,13 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
148139
done();
149140
};
150141

151-
verdaccioGitlab.allow_publish(user, _package, cb);
142+
verdaccioGitlab.allow_publish(TEST_REMOTE_USER, _package, cb);
152143
});
153144

154145
test('should allow publish of package based on user name', done => {
155-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
156-
const user: RemoteUser = {
157-
real_groups: ['myGroup', 'myUser'],
158-
groups: ['myGroup', 'myUser'],
159-
name: 'myUser'
160-
};
146+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
161147
const _package: VerdaccioGitlabPackageAccess = {
162-
name: 'myUser',
148+
name: TEST_USER,
163149
gitlab: true
164150
};
165151

@@ -169,18 +155,18 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
169155
done();
170156
};
171157

172-
verdaccioGitlab.allow_publish(user, _package, cb);
158+
verdaccioGitlab.allow_publish(TEST_REMOTE_USER, _package, cb);
173159
});
174160

175161
test('should deny publish of package based on unauthenticated', done => {
176-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
177-
const user: RemoteUser = {
162+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
163+
const unauthenticatedUser: RemoteUser = {
178164
real_groups: [],
179165
groups: [],
180166
name: undefined
181167
};
182168
const _package: VerdaccioGitlabPackageAccess = {
183-
name: 'myUser',
169+
name: TEST_USER,
184170
gitlab: true
185171
};
186172

@@ -190,16 +176,11 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
190176
done();
191177
};
192178

193-
verdaccioGitlab.allow_publish(user, _package, cb);
179+
verdaccioGitlab.allow_publish(unauthenticatedUser, _package, cb);
194180
});
195181

196182
test('should deny publish of package based on group', done => {
197-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
198-
const user: RemoteUser = {
199-
real_groups: ['myGroup', 'myUser'],
200-
groups: ['myGroup', 'myUser'],
201-
name: 'myUser'
202-
};
183+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
203184
const _package: VerdaccioGitlabPackageAccess = {
204185
name: '@anotherGroup/myPackage',
205186
gitlab: true
@@ -211,16 +192,11 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
211192
done();
212193
};
213194

214-
verdaccioGitlab.allow_publish(user, _package, cb);
195+
verdaccioGitlab.allow_publish(TEST_REMOTE_USER, _package, cb);
215196
});
216197

217198
test('should deny publish of package based on user', done => {
218-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
219-
const user: RemoteUser = {
220-
real_groups: ['myGroup', 'myUser'],
221-
groups: ['myGroup', 'myUser'],
222-
name: 'myUser'
223-
};
199+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, TEST_OPTIONS);
224200
const _package: VerdaccioGitlabPackageAccess = {
225201
name: 'anotherUser',
226202
gitlab: true
@@ -232,6 +208,6 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
232208
done();
233209
};
234210

235-
verdaccioGitlab.allow_publish(user, _package, cb);
211+
verdaccioGitlab.allow_publish(TEST_REMOTE_USER, _package, cb);
236212
});
237213
});

test/unit/partials/config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ import type { VerdaccioGitlabConfig } from '../../../src/gitlab.js';
44

55

66
export const defaultConfig: VerdaccioGitlabConfig = {
7-
url: 'myUrl',
8-
7+
url: 'myUrl'
98
};

0 commit comments

Comments
 (0)