1 module tests.management.profile; 2 3 import tests.management.setup; 4 5 /// It should render 404 when the user does not exist 6 unittest { 7 auto router = testRouter; 8 collection.empower("user@gmail.com", "admin"); 9 10 router 11 .request 12 .get("/admin/users/3") 13 .expectStatusCode(404) 14 .end(); 15 } 16 17 /// It should update the user data 18 unittest { 19 testRouter 20 .request 21 .post("/admin/users/1/update") 22 .header("Cookie", "auth-token=" ~ authToken.name) 23 .send(["name": " some name ", "username": " some-user-name "]) 24 .expectStatusCode(302) 25 .expectHeader("Location", "http://localhost:0/admin/users/1?message=Profile%20updated%20successfully.") 26 .end((Response response) => { 27 auto user = collection.byId("1"); 28 user.name.should.equal("some name"); 29 user.username.should.equal("some-user-name"); 30 }); 31 } 32 33 /// It should not be able to update the username to an existing one 34 unittest { 35 auto router = testRouter; 36 collection.empower("user@gmail.com", "admin"); 37 38 auto user = new User("user2@gmail.com", "password"); 39 user.name = "John Doe"; 40 user.username = "other test"; 41 user.id = 2; 42 43 collection.add(user); 44 45 router 46 .request 47 .post("/admin/users/2/update") 48 .header("Cookie", "auth-token=" ~ authToken.name) 49 .send(["name": " some name ", "username": "test"]) 50 .expectStatusCode(302) 51 .expectHeader("Location", "http://localhost:0/admin/users/2?error=The%20new%20username%20is%20already%20taken.") 52 .end((Response response) => { 53 auto user = collection.byId("2"); 54 user.name.should.equal("John Doe"); 55 user.username.should.equal("other test"); 56 }); 57 } 58 59 /// It should not update the user data when the name is missing 60 unittest { 61 testRouter 62 .request 63 .post("/admin/users/1/update") 64 .header("Cookie", "auth-token=" ~ authToken.name) 65 .send(["username": "some user name"]) 66 .expectStatusCode(302) 67 .expectHeader("Location", "http://localhost:0/admin/users/1?error=Missing%20data.%20The%20request%20can%20not%20be%20processed.") 68 .end((Response response) => { 69 auto user = collection.byId("1"); 70 user.name.should.equal("John Doe"); 71 user.username.should.equal("test"); 72 }); 73 } 74 75 /// It should not update the user data when the name is missing 76 unittest { 77 testRouter 78 .request 79 .post("/admin/users/1/update") 80 .header("Cookie", "auth-token=" ~ authToken.name) 81 .send(["name": "name"]) 82 .expectStatusCode(302) 83 .expectHeader("Location", "http://localhost:0/admin/users/1?error=Missing%20data.%20The%20request%20can%20not%20be%20processed.") 84 .end((Response response) => { 85 auto user = collection.byId("1"); 86 user.name.should.equal("John Doe"); 87 user.username.should.equal("test"); 88 }); 89 } 90 91 /// It should not update the user data when the username is empty 92 unittest { 93 testRouter 94 .request 95 .post("/admin/users/1/update") 96 .header("Cookie", "auth-token=" ~ authToken.name) 97 .send(["name": "", "username": ""]) 98 .expectStatusCode(302) 99 .expectHeader("Location", "http://localhost:0/admin/users/1?error=The%20username%20is%20mandatory.") 100 .end((Response response) => { 101 auto user = collection.byId("1"); 102 user.name.should.equal("John Doe"); 103 user.username.should.equal("test"); 104 }); 105 } 106 107 /// It should escape the user data inputs 108 unittest { 109 testRouter 110 .request 111 .post("/admin/users/1/update") 112 .header("Cookie", "auth-token=" ~ authToken.name) 113 .send(["name": "\"'<>", "username": "Asd"]) 114 .expectStatusCode(302) 115 .expectHeader("Location", "http://localhost:0/admin/users/1?message=Profile%20updated%20successfully.") 116 .end((Response response) => { 117 auto user = collection.byId("1"); 118 user.name.should.equal(""'<>"); 119 }); 120 }