This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex3.test.js
96 lines (86 loc) · 2.71 KB
/
index3.test.js
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
'use strict';
var fs = require('jest-plugin-fs').default;
jest.mock('fs', () => require('jest-plugin-fs/mock'));
var request = require('supertest');
var express = require('express');
var plugin = require('./index')({}, {});
var locker = request('@verdaccio/file-locking');
var mock_authenticate = jest.fn((user, password, cb) => {
cb(null, (user === 'adam3' && password === 'Welcome1') ? [user] : false);
});
var mock_basic_middleware = jest.fn((req, res, next) => {
next();
});
function HTPasswd() {
this.config = { file: '/mocked_htpasswd' };
};
HTPasswd.prototype.authenticate = mock_authenticate;
var auth = {
plugins: [new HTPasswd()],
apiJWTmiddleware: () => mock_basic_middleware
}
var app = express();
plugin.register_middlewares(app, auth, undefined);
app.use((body, req, res, next) => {
res.header('Content-type', 'application/json');
res.send(JSON.stringify(body));
});
describe('verdaccio 3.x', () => {
beforeEach(() => fs.mock({"/mocked_htpasswd": "adam3:$6abcdefghijk:autocreated 2018-06-04T21:47:11.284Z\n"}));
afterEach(() => fs.restore());
describe('unauthenticated', () => {
test('GET /-/npm/v1/user', (done) => {
request(app).
get('/-/npm/v1/user').
expect(403, done);
});
test('POST /-/npm/v1/user', done => {
request(app).
post('/-/npm/v1/user').
send({password: {old: 'Password1', new: 'Welcome1'}}).
expect(403, done);
});
});
describe('authenticated', () => {
beforeEach(() => {
mock_basic_middleware.mockImplementation((req, res, next) => {
req.remote_user = {name: 'adam3'};
next();
});
});
test('GET /-/npm/v1/user', (done) => {
request(app).
get('/-/npm/v1/user').
expect(200, {name: 'adam3'}, done);
});
describe('POST /-/npm/v1/user', () => {
test('missing change password data', done => {
request(app).
post('/-/npm/v1/user').
send({}).
expect(501, done);
});
test('wrong old password', done => {
request(app).
post('/-/npm/v1/user').
send({password: {old: 'wrong', new: 'Password1'}}).
expect(403, done);
});
test('password changed', done => {
request(app).
post('/-/npm/v1/user').
send({password: {old: 'Welcome1', new: 'Password1'}}).
expect(200, {name: 'adam3'}, done);
});
test('authenticate returns error', done => {
mock_authenticate.mockImplementation((usr, pwd, cb) => {
cb(new Error('mocked error'));
});
request(app).
post('/-/npm/v1/user').
send({password: {old: 'Welcome1', new: 'Password1'}}).
expect(500, done);
});
});
});
});