-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_register_test.go
66 lines (53 loc) · 1.34 KB
/
user_register_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package auth_test
import (
"database/sql"
"errors"
"os"
"testing"
"github.com/cristosal/auth"
_ "github.com/jackc/pgx/v5/stdlib"
)
func TestRegister(t *testing.T) {
// the problem here is we should use sqlmock
conn, err := sql.Open("pgx", os.Getenv("CONNECTION_STRING"))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err = conn.Exec("delete from users where email = $1", "[email protected]")
if err != nil {
t.Fatal(err)
}
s := auth.NewService(conn)
req := &auth.RegistrationRequest{
Name: "pepe ",
Email: " [email protected] ",
Phone: "hello world",
Password: " 123 ",
}
reg, err := s.Users().Register(req)
if err != nil {
t.Fatalf("unable to register %v", err)
}
_, err = s.Users().Register(req)
if !errors.Is(err, auth.ErrUserExists) {
t.Fatalf("expected user exists got %v", err)
}
if reg.Name != "pepe" {
t.Fatal("expected to name to be sanitized")
}
if reg.Email != "[email protected]" {
t.Fatal("expected email to be sanitized")
}
if reg.Token == "" {
t.Fatal("expected token to be present")
}
_, err = s.Users().ConfirmRegistration("fail")
if !errors.Is(err, auth.ErrInvalidToken) {
t.Fatalf("expected invalid token got %v", err)
}
_, err = s.Users().ConfirmRegistration(reg.Token)
if err != nil {
t.Fatal("expected confirmation of token to not return anything")
}
}