Skip to content

Commit 435521b

Browse files
Merge pull request #48 from bufferoverflow/chore/misc-corrections
chore: misc corrections
2 parents adcef60 + d81439d commit 435521b

File tree

5 files changed

+89
-113
lines changed

5 files changed

+89
-113
lines changed

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: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
// @flow
22

3-
import type { Logger } from '@verdaccio/types';
4-
import type { UserDataGroups } from '../../src/authcache.js';
5-
63
import { AuthCache, UserData } from '../../src/authcache.js';
4+
import logger from './partials/logger.js';
5+
import config from './partials/config';
76

87

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()
17-
};
18-
198
describe('AuthCache Unit Tests', () => {
209
test('should create an AuthCache instance', () => {
2110
const authCache: AuthCache = new AuthCache(logger, AuthCache.DEFAULT_TTL);
@@ -32,32 +21,20 @@ describe('AuthCache Unit Tests', () => {
3221

3322
test('should store and find some user data', () => {
3423
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);
4124

42-
authCache.storeUser(user, pass, data);
43-
const returnedData: UserData = authCache.findUser(user, pass);
25+
authCache.storeUser(config.user, config.pass, config.userData);
26+
const returnedData: UserData = authCache.findUser(config.user, config.pass);
4427

45-
expect(returnedData).toEqual(data);
28+
expect(returnedData).toEqual(config.userData);
4629
});
4730

4831
test('should store and find some user data when ttl is unlimited', () => {
4932
const UNLIMITED_TTL: number = 0;
5033
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);
5734

58-
authCache.storeUser(user, pass, data);
59-
const returnedData: UserData = authCache.findUser(user, pass);
35+
authCache.storeUser(config.user, config.pass, config.userData);
36+
const returnedData: UserData = authCache.findUser(config.user, config.pass);
6037

61-
expect(returnedData).toEqual(data);
38+
expect(returnedData).toEqual(config.userData);
6239
});
6340
});

test/unit/gitlab.spec.js

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,60 @@ import type { Callback, RemoteUser } from '@verdaccio/types';
44
import type { VerdaccioGitlabPackageAccess } from "../../src/gitlab";
55

66
import VerdaccioGitlab from '../../src/gitlab.js';
7-
import { defaultConfig } from './partials/config.js';
8-
import logger from './partials/logger.js';
7+
import config from './partials/config';
98

109
// Do not remove, this mocks the gitlab library
1110
import Gitlab from 'gitlab'; // eslint-disable-line no-unused-vars
12-
const options = {
13-
// $FlowFixMe
14-
config: {},
15-
logger: logger
16-
};
11+
12+
1713

1814
describe('Gitlab Auth Plugin Unit Tests', () => {
1915
test('should create a plugin instance', () => {
20-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
16+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
2117

2218
expect(verdaccioGitlab).toBeDefined();
2319
});
2420

2521
test('should authenticate a user', done => {
26-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
27-
const user: string = 'myUser';
28-
const pass: string = 'myPass';
22+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
2923

3024
const cb: Callback = (err, data) => {
3125
expect(err).toBeFalsy();
3226
expect(data.sort()).toEqual(['myGroup', 'myUser'].sort());
3327
done();
3428
};
3529

36-
verdaccioGitlab.authenticate(user, pass, cb);
30+
verdaccioGitlab.authenticate(config.user, config.pass, cb);
3731
});
3832

3933
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';
34+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
35+
const wrongPass: string = config.pass + '_wrong';
4336

4437
const cb: Callback = (err, data) => {
4538
expect(err).toBeTruthy();
4639
expect(data).toBeFalsy();
4740
done();
4841
};
4942

50-
verdaccioGitlab.authenticate(user, pass, cb);
43+
verdaccioGitlab.authenticate(config.user, wrongPass, cb);
5144
});
5245

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';
46+
test('should fail authentication with non-existing user', done => {
47+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
48+
const wrongUser: string = config.user + '_wrong';
5749

5850
const cb: Callback = (err, data) => {
5951
expect(err).toBeTruthy();
6052
expect(data).toBeFalsy();
6153
done();
6254
};
6355

64-
verdaccioGitlab.authenticate(user, pass, cb);
56+
verdaccioGitlab.authenticate(wrongUser, config.pass, cb);
6557
});
6658

6759
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-
};
60+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
7461
const _package: VerdaccioGitlabPackageAccess = {
7562
name: '@myGroup/myPackage',
7663
access: ['$authenticated'],
@@ -83,18 +70,13 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
8370
done();
8471
};
8572

86-
verdaccioGitlab.allow_access(user, _package, cb);
73+
verdaccioGitlab.allow_access(config.remoteUser, _package, cb);
8774
});
8875

8976
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-
};
77+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
9678
const _package: VerdaccioGitlabPackageAccess = {
97-
name: 'myUser',
79+
name: config.user,
9880
access: ['$authenticated'],
9981
gitlab: true
10082
};
@@ -105,12 +87,12 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
10587
done();
10688
};
10789

108-
verdaccioGitlab.allow_access(user, _package, cb);
90+
verdaccioGitlab.allow_access(config.remoteUser, _package, cb);
10991
});
11092

11193
test('should deny access to package based on unauthenticated', done => {
112-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
113-
const user: RemoteUser = {
94+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
95+
const unauthenticatedUser: RemoteUser = {
11496
real_groups: [],
11597
groups: [],
11698
name: undefined
@@ -127,16 +109,11 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
127109
done();
128110
};
129111

130-
verdaccioGitlab.allow_access(user, _package, cb);
112+
verdaccioGitlab.allow_access(unauthenticatedUser, _package, cb);
131113
});
132114

133115
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-
};
116+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
140117
const _package: VerdaccioGitlabPackageAccess = {
141118
name: '@myGroup/myPackage',
142119
gitlab: true
@@ -148,18 +125,13 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
148125
done();
149126
};
150127

151-
verdaccioGitlab.allow_publish(user, _package, cb);
128+
verdaccioGitlab.allow_publish(config.remoteUser, _package, cb);
152129
});
153130

154131
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-
};
132+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
161133
const _package: VerdaccioGitlabPackageAccess = {
162-
name: 'myUser',
134+
name: config.user,
163135
gitlab: true
164136
};
165137

@@ -169,18 +141,18 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
169141
done();
170142
};
171143

172-
verdaccioGitlab.allow_publish(user, _package, cb);
144+
verdaccioGitlab.allow_publish(config.remoteUser, _package, cb);
173145
});
174146

175147
test('should deny publish of package based on unauthenticated', done => {
176-
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(defaultConfig, options);
177-
const user: RemoteUser = {
148+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
149+
const unauthenticatedUser: RemoteUser = {
178150
real_groups: [],
179151
groups: [],
180152
name: undefined
181153
};
182154
const _package: VerdaccioGitlabPackageAccess = {
183-
name: 'myUser',
155+
name: config.user,
184156
gitlab: true
185157
};
186158

@@ -190,16 +162,11 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
190162
done();
191163
};
192164

193-
verdaccioGitlab.allow_publish(user, _package, cb);
165+
verdaccioGitlab.allow_publish(unauthenticatedUser, _package, cb);
194166
});
195167

196168
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-
};
169+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
203170
const _package: VerdaccioGitlabPackageAccess = {
204171
name: '@anotherGroup/myPackage',
205172
gitlab: true
@@ -211,16 +178,11 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
211178
done();
212179
};
213180

214-
verdaccioGitlab.allow_publish(user, _package, cb);
181+
verdaccioGitlab.allow_publish(config.remoteUser, _package, cb);
215182
});
216183

217184
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-
};
185+
const verdaccioGitlab: VerdaccioGitlab = new VerdaccioGitlab(config.verdaccioGitlabConfig, config.options);
224186
const _package: VerdaccioGitlabPackageAccess = {
225187
name: 'anotherUser',
226188
gitlab: true
@@ -232,6 +194,6 @@ describe('Gitlab Auth Plugin Unit Tests', () => {
232194
done();
233195
};
234196

235-
verdaccioGitlab.allow_publish(user, _package, cb);
197+
verdaccioGitlab.allow_publish(config.remoteUser, _package, cb);
236198
});
237199
});

test/unit/partials/config.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/unit/partials/config/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@flow
2+
3+
import type { PluginOptions, RemoteUser } from '@verdaccio/types';
4+
import type { VerdaccioGitlabConfig } from '../../../../src/gitlab.js';
5+
import type { UserDataGroups } from '../../../../src/authcache.js';
6+
7+
import logger from '../logger.js';
8+
import { UserData } from '../../../../src/authcache.js';
9+
10+
11+
const verdaccioGitlabConfig: VerdaccioGitlabConfig = {
12+
url: 'myUrl'
13+
};
14+
15+
const options: PluginOptions = {
16+
// $FlowFixMe
17+
config: {},
18+
logger: logger
19+
};
20+
21+
const user: string = 'myUser';
22+
const pass: string = 'myPass';
23+
const remoteUser: RemoteUser = {
24+
real_groups: ['myGroup', user],
25+
groups: ['myGroup', user],
26+
name: user
27+
};
28+
29+
const userDataGroups: UserDataGroups = {
30+
publish: ['fooGroup1', 'fooGroup2']
31+
};
32+
const userData: UserData = new UserData(user, userDataGroups);
33+
34+
const config = {
35+
verdaccioGitlabConfig: verdaccioGitlabConfig,
36+
options: options,
37+
user: user,
38+
pass: pass,
39+
remoteUser: remoteUser,
40+
userData: userData
41+
}
42+
43+
export default config;

0 commit comments

Comments
 (0)