diff options
Diffstat (limited to 'server/tests/api')
-rw-r--r-- | server/tests/api/check-params/users.ts | 22 | ||||
-rw-r--r-- | server/tests/api/index-fast.ts | 1 | ||||
-rw-r--r-- | server/tests/api/server/email.ts | 94 |
3 files changed, 117 insertions, 0 deletions
diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index 28fefe79f..0fbc414c9 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts | |||
@@ -541,6 +541,28 @@ describe('Test users API validators', function () { | |||
541 | }) | 541 | }) |
542 | }) | 542 | }) |
543 | 543 | ||
544 | describe('When asking a password reset', function () { | ||
545 | const path = '/api/v1/users/ask-reset-password' | ||
546 | |||
547 | it('Should fail with a missing email', async function () { | ||
548 | const fields = {} | ||
549 | |||
550 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | ||
551 | }) | ||
552 | |||
553 | it('Should fail with an invalid email', async function () { | ||
554 | const fields = { email: 'hello' } | ||
555 | |||
556 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) | ||
557 | }) | ||
558 | |||
559 | it('Should success with the correct params', async function () { | ||
560 | const fields = { email: 'admin@example.com' } | ||
561 | |||
562 | await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 }) | ||
563 | }) | ||
564 | }) | ||
565 | |||
544 | after(async function () { | 566 | after(async function () { |
545 | killallServers([ server, serverWithRegistrationDisabled ]) | 567 | killallServers([ server, serverWithRegistrationDisabled ]) |
546 | 568 | ||
diff --git a/server/tests/api/index-fast.ts b/server/tests/api/index-fast.ts index e591d0fd2..9f52310dd 100644 --- a/server/tests/api/index-fast.ts +++ b/server/tests/api/index-fast.ts | |||
@@ -9,3 +9,4 @@ import './videos/video-blacklist-management' | |||
9 | import './videos/video-description' | 9 | import './videos/video-description' |
10 | import './videos/video-privacy' | 10 | import './videos/video-privacy' |
11 | import './videos/services' | 11 | import './videos/services' |
12 | import './server/email' | ||
diff --git a/server/tests/api/server/email.ts b/server/tests/api/server/email.ts new file mode 100644 index 000000000..8eb9c0fa4 --- /dev/null +++ b/server/tests/api/server/email.ts | |||
@@ -0,0 +1,94 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | ||
2 | |||
3 | import * as chai from 'chai' | ||
4 | import 'mocha' | ||
5 | import { askResetPassword, createUser, resetPassword, runServer, userLogin, wait } from '../../utils' | ||
6 | import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index' | ||
7 | import { mockSmtpServer } from '../../utils/miscs/email' | ||
8 | |||
9 | const expect = chai.expect | ||
10 | |||
11 | describe('Test emails', function () { | ||
12 | let server: ServerInfo | ||
13 | let userId: number | ||
14 | let verificationString: string | ||
15 | const emails: object[] = [] | ||
16 | const user = { | ||
17 | username: 'user_1', | ||
18 | password: 'super_password' | ||
19 | } | ||
20 | |||
21 | before(async function () { | ||
22 | this.timeout(30000) | ||
23 | |||
24 | await mockSmtpServer(emails) | ||
25 | |||
26 | await flushTests() | ||
27 | |||
28 | const overrideConfig = { | ||
29 | smtp: { | ||
30 | hostname: 'localhost' | ||
31 | } | ||
32 | } | ||
33 | server = await runServer(1, overrideConfig) | ||
34 | |||
35 | await wait(5000) | ||
36 | await setAccessTokensToServers([ server ]) | ||
37 | |||
38 | const res = await createUser(server.url, server.accessToken, user.username, user.password) | ||
39 | userId = res.body.user.id | ||
40 | }) | ||
41 | |||
42 | describe('When resetting user password', function () { | ||
43 | |||
44 | it('Should ask to reset the password', async function () { | ||
45 | this.timeout(10000) | ||
46 | |||
47 | await askResetPassword(server.url, 'user_1@example.com') | ||
48 | |||
49 | await wait(3000) | ||
50 | expect(emails).to.have.lengthOf(1) | ||
51 | |||
52 | const email = emails[0] | ||
53 | |||
54 | expect(email['from'][0]['address']).equal('test-admin@localhost') | ||
55 | expect(email['to'][0]['address']).equal('user_1@example.com') | ||
56 | expect(email['subject']).contains('password') | ||
57 | |||
58 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | ||
59 | expect(verificationStringMatches).not.to.be.null | ||
60 | |||
61 | verificationString = verificationStringMatches[1] | ||
62 | expect(verificationString).to.have.length.above(2) | ||
63 | |||
64 | const userIdMatches = /userId=([0-9]+)/.exec(email['text']) | ||
65 | expect(userIdMatches).not.to.be.null | ||
66 | |||
67 | userId = parseInt(userIdMatches[1], 10) | ||
68 | expect(verificationString).to.not.be.undefined | ||
69 | }) | ||
70 | |||
71 | it('Should not reset the password with an invalid verification string', async function () { | ||
72 | await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403) | ||
73 | }) | ||
74 | |||
75 | it('Should reset the password', async function () { | ||
76 | await resetPassword(server.url, userId, verificationString, 'super_password2') | ||
77 | }) | ||
78 | |||
79 | it('Should login with this new password', async function () { | ||
80 | user.password = 'super_password2' | ||
81 | |||
82 | await userLogin(server, user) | ||
83 | }) | ||
84 | }) | ||
85 | |||
86 | after(async function () { | ||
87 | killallServers([ server ]) | ||
88 | |||
89 | // Keep the logs if the test failed | ||
90 | if (this['ok']) { | ||
91 | await flushTests() | ||
92 | } | ||
93 | }) | ||
94 | }) | ||