@@ -2,17 +2,24 @@ defmodule CodeCorpsWeb.PasswordResetControllerTest do
22 @ moduledoc false
33
44 use CodeCorpsWeb.ApiCase , resource_name: :password_reset
5+ import CodeCorps.TestEnvironmentHelper
56 alias CodeCorps . { User , AuthToken }
67
7- test "updates user password when data is valid" , % { conn: conn } do
8+ test "updates user password when data is valid and deletes auth token model " , % { conn: conn } do
89 current_user = insert ( :user )
910 { :ok , auth_token } = AuthToken . changeset ( % AuthToken { } , current_user ) |> Repo . insert
11+
12+ assert AuthToken |> Repo . get ( auth_token . id ) |> Map . get ( :id ) == auth_token . id
13+
1014 attrs = % { "token" => auth_token . value , "password" => "123456" , "password_confirmation" => "123456" }
1115 conn = post conn , password_reset_path ( conn , :reset_password ) , attrs
1216 response = json_response ( conn , 201 )
17+
1318 assert response
1419 encrypted_password = Repo . get ( User , current_user . id ) . encrypted_password
20+
1521 assert Comeonin.Bcrypt . checkpw ( "123456" , encrypted_password )
22+ assert AuthToken |> Repo . get ( auth_token . id ) == nil
1623 end
1724
1825 test "does not create resource and renders errors when password does not match" , % { conn: conn } do
@@ -21,6 +28,7 @@ defmodule CodeCorpsWeb.PasswordResetControllerTest do
2128 attrs = % { "token" => auth_token . value , "password" => "123456" , "password_confirmation" => "another" }
2229 conn = post conn , password_reset_path ( conn , :reset_password ) , attrs
2330 response = json_response ( conn , 422 )
31+
2432 assert % { "errors" => [ % { "detail" => "Password confirmation passwords do not match" } ] } = response
2533 end
2634
@@ -29,6 +37,17 @@ defmodule CodeCorpsWeb.PasswordResetControllerTest do
2937 { :ok , _ } = AuthToken . changeset ( % AuthToken { } , current_user ) |> Repo . insert
3038 attrs = % { "token" => "random token" , "password" => "123456" , "password_confirmation" => "123456" }
3139 conn = post conn , password_reset_path ( conn , :reset_password ) , attrs
40+
41+ assert json_response ( conn , 404 )
42+ end
43+
44+ test "does not create resource and renders errors when error in token timeout occurs" , % { conn: conn } do
45+ modify_env ( :code_corps , password_reset_timeout: 0 )
46+
47+ current_user = insert ( :user )
48+ { :ok , auth_token } = AuthToken . changeset ( % AuthToken { } , current_user ) |> Repo . insert
49+ attrs = % { "token" => auth_token . value , "password" => "123456" , "password_confirmation" => "123456" }
50+ conn = post conn , password_reset_path ( conn , :reset_password ) , attrs
3251 assert json_response ( conn , 404 )
3352 end
3453
0 commit comments