@@ -3,6 +3,7 @@ package controllertests
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"log"
8
9
"net/http"
@@ -18,18 +19,42 @@ func TestSignIn(t *testing.T) {
18
19
if err != nil {
19
20
log .Fatal (err )
20
21
}
21
-
22
22
user , err := seedOneUser ()
23
23
if err != nil {
24
24
fmt .Printf ("This is the error %v\n " , err )
25
25
}
26
- fmt .Printf ("This is the user: %v\n " , user )
27
26
28
- token , err := server .SignIn (user .Email , user .Password )
29
- if err != nil {
30
- log .Fatal (err )
27
+ samples := []struct {
28
+ email string
29
+ password string
30
+ errorMessage string
31
+ }{
32
+ {
33
+ email : user .Email ,
34
+ password : "password" , //Note the password has to be this, not the hashed one from the database
35
+ errorMessage : "" ,
36
+ },
37
+ {
38
+ email : user .Email ,
39
+ password : "Wrong password" ,
40
+ errorMessage : "crypto/bcrypt: hashedPassword is not the hash of the given password" ,
41
+ },
42
+ {
43
+ email : "Wrong email" ,
44
+ password : "password" ,
45
+ errorMessage : "record not found" ,
46
+ },
47
+ }
48
+
49
+ for _ , v := range samples {
50
+
51
+ token , err := server .SignIn (v .email , v .password )
52
+ if err != nil {
53
+ assert .Equal (t , err , errors .New (v .errorMessage ))
54
+ } else {
55
+ assert .NotEqual (t , token , "" )
56
+ }
31
57
}
32
- assert .NotEqual (t , token , "" )
33
58
}
34
59
35
60
func TestLogin (t * testing.T ) {
@@ -40,7 +65,6 @@ func TestLogin(t *testing.T) {
40
65
if err != nil {
41
66
fmt .Printf ("This is the error %v\n " , err )
42
67
}
43
-
44
68
samples := []struct {
45
69
inputJSON string
46
70
statusCode int
@@ -53,10 +77,15 @@ func TestLogin(t *testing.T) {
53
77
statusCode : 200 ,
54
78
errorMessage : "" ,
55
79
},
80
+ {
81
+ inputJSON : `{"email": "pet@gmail.com", "password": "wrong password"}` ,
82
+ statusCode : 422 ,
83
+ errorMessage : "Incorrect Password" ,
84
+ },
56
85
{
57
86
inputJSON : `{"email": "frank@gmail.com", "password": "password"}` ,
58
87
statusCode : 422 ,
59
- errorMessage : "record not found " ,
88
+ errorMessage : "Incorrect Details " ,
60
89
},
61
90
{
62
91
inputJSON : `{"email": "kangmail.com", "password": "password"}` ,
@@ -84,7 +113,7 @@ func TestLogin(t *testing.T) {
84
113
85
114
req , err := http .NewRequest ("POST" , "/login" , bytes .NewBufferString (v .inputJSON ))
86
115
if err != nil {
87
- log . Fatalf ("this is the error: %v" , err )
116
+ t . Errorf ("this is the error: %v" , err )
88
117
}
89
118
rr := httptest .NewRecorder ()
90
119
handler := http .HandlerFunc (server .Login )
@@ -99,7 +128,7 @@ func TestLogin(t *testing.T) {
99
128
responseMap := make (map [string ]interface {})
100
129
err = json .Unmarshal ([]byte (rr .Body .String ()), & responseMap )
101
130
if err != nil {
102
- fmt . Printf ("Cannot convert to json: %v" , err )
131
+ t . Errorf ("Cannot convert to json: %v" , err )
103
132
}
104
133
assert .Equal (t , responseMap ["error" ], v .errorMessage )
105
134
}
0 commit comments