1 module tests.management.account;
2 
3 import tests.management.setup;
4 
5 /// It should change the user password
6 unittest {
7   testRouter
8     .request
9     .post("/admin/users/1/account/update")
10     .header("Cookie", "auth-token=" ~ authToken.name)
11     .send(["oldPassword": "password", "newPassword": "new-password", "confirmPassword": "new-password"])
12     .expectStatusCode(302)
13     .expectHeader("Location", "http://localhost:0/admin/users/1/account?message=Password%20updated%20successfully.")
14     .end((Response response) => {
15       collection.byId("1").isValidPassword("new-password").should.equal(true);
16     });
17 }
18 
19 /// It should not change the user password when the old is not valid
20 unittest {
21   testRouter
22     .request
23     .post("/admin/users/1/account/update")
24     .header("Cookie", "auth-token=" ~ authToken.name)
25     .send(["oldPassword": "wrong password", "newPassword": "new-password", "confirmPassword": "new-password"])
26     .expectStatusCode(302)
27     .expectHeader("Location", "http://localhost:0/admin/users/1/account?error=The%20old%20password%20is%20not%20valid.")
28     .end((Response response) => {
29       collection.byId("1").isValidPassword("password").should.equal(true);
30     });
31 }
32 
33 /// It should not change the user password when newPassword does not match confirmation
34 unittest {
35   testRouter
36     .request
37     .post("/admin/users/1/account/update")
38     .header("Cookie", "auth-token=" ~ authToken.name)
39     .send(["oldPassword": "password", "newPassword": "new-password", "confirmPassword": "some-password"])
40     .expectStatusCode(302)
41     .expectHeader("Location", "http://localhost:0/admin/users/1/account?error=Password%20confirmation%20doesn't%20match%20the%20password.")
42     .end((Response response) => {
43       collection.byId("1").isValidPassword("password").should.equal(true);
44     });
45 }
46 
47 /// It should not change the user password when there are missing form data
48 unittest {
49   testRouter
50     .request
51     .post("/admin/users/1/account/update")
52     .header("Cookie", "auth-token=" ~ authToken.name)
53     .send(["":""])
54     .expectStatusCode(302)
55     .expectHeader("Location", "http://localhost:0/admin/users/1/account?error=oldPassword%20newPassword%20confirmPassword%20fields%20are%20missing.")
56     .end((Response response) => {
57       collection.byId("1").isValidPassword("password").should.equal(true);
58     });
59 }
60 
61 /// It should not change the user password when newPassword is less than 10 chars
62 unittest {
63   testRouter
64     .request
65     .post("/admin/users/1/account/update")
66     .header("Cookie", "auth-token=" ~ authToken.name)
67     .send(["oldPassword": "password", "newPassword": "new", "confirmPassword": "new"])
68     .expectStatusCode(302)
69     .expectHeader("Location", "http://localhost:0/admin/users/1/account?error=The%20new%20password%20is%20less%20then%2010%20chars.")
70     .end((Response response) => {
71       collection.byId("1").isValidPassword("password").should.equal(true);
72     });
73 }