1 | "use strict";
|
2 |
|
3 | const assert = require('assert');
|
4 | const describeConfig = require('../setupTester');
|
5 | const EmailAuth = require('./EmailAuth');
|
6 | const PBKDF2 = require('../crypt/PBKDF2');
|
7 |
|
8 | async function config()
|
9 | {
|
10 | const cfg = {};
|
11 |
|
12 | cfg.users = await describeConfig.createUsers([{
|
13 | id: 'regular',
|
14 | password: 'INVALID'
|
15 | }, {
|
16 | id: 'admin',
|
17 | roles: {
|
18 | admin: true
|
19 | }
|
20 | }]);
|
21 |
|
22 | cfg.crypt = new PBKDF2();
|
23 |
|
24 | cfg.auth = [
|
25 | new EmailAuth(cfg)
|
26 | ];
|
27 |
|
28 | return cfg;
|
29 | }
|
30 |
|
31 | describeConfig('config', config, function (handler)
|
32 | {
|
33 | it('should have login methods', async function ()
|
34 | {
|
35 | const
|
36 | {
|
37 | status,
|
38 | data
|
39 | } = await handler.request('GET', '/api/accounts/methods.json');
|
40 | assert.deepEqual(status, 200);
|
41 | assert.notDeepEqual(data, []);
|
42 | });
|
43 |
|
44 | it('should fail bad input', async function ()
|
45 | {
|
46 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}))
|
47 | .status, 400);
|
48 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {}))
|
49 | .status, 400);
|
50 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {
|
51 | username: ''
|
52 | }))
|
53 | .status, 400);
|
54 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {
|
55 | username: 'a'
|
56 | }))
|
57 | .status, 400);
|
58 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {
|
59 | password: ''
|
60 | }))
|
61 | .status, 400);
|
62 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {
|
63 | username: 'regular',
|
64 | password: ''
|
65 | }))
|
66 | .status, 400);
|
67 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {
|
68 | username: 'regular',
|
69 | password: 'XXX'
|
70 | }))
|
71 | .status, 400);
|
72 | assert.deepEqual((await handler.request('POST', '/api/accounts/email/login.json', {}, {
|
73 | username: 'admin',
|
74 | password: 'XXX'
|
75 | }))
|
76 | .status, 400);
|
77 |
|
78 | });
|
79 | });
|
80 |
|
81 | describe(EmailAuth.name, function ()
|
82 | {
|
83 | let instance;
|
84 | let emails = [];
|
85 | beforeEach(function ()
|
86 | {
|
87 | emails = [];
|
88 | instance = new EmailAuth({
|
89 | crypt: new PBKDF2(),
|
90 | emailSender: {
|
91 | send: function ()
|
92 | {
|
93 | emails.push(Array.prototype.slice.call(arguments))
|
94 | }
|
95 | }
|
96 | });
|
97 | });
|
98 |
|
99 | it(`should create profile from id`, async function ()
|
100 | {
|
101 | await instance.createProfileFromEmail('test@test');
|
102 | await instance.createProfileFromEmail('test@test', {
|
103 | password: 'xxx'
|
104 | });
|
105 |
|
106 | instance.createUserFromProfile(await instance.createProfileFromEmail('test@test', {
|
107 | password: 'xxx'
|
108 | }));
|
109 |
|
110 | instance.allowPasswordSettingDuringRegistration = true;
|
111 |
|
112 | instance.createUserFromProfile(await instance.createProfileFromEmail('test@test', {
|
113 | password: 'xxx'
|
114 | }));
|
115 | })
|
116 |
|
117 | it(`should send email`, async function ()
|
118 | {
|
119 | await instance.sendTemporaryPassword('', 'email', 'password', 'expireMinutes');
|
120 | await instance.sendTemporaryPassword('', 'email', 'password', 'expireMinutes', 'loginLinkPrefix');
|
121 | await instance.sendTemporaryPassword('register', 'email', 'password', 'expireMinutes', 'loginLinkPrefix');
|
122 | await instance.sendTemporaryPassword('recover', 'email', 'password', 'expireMinutes', 'loginLinkPrefix');
|
123 | })
|
124 | })
|