-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
117 lines (111 loc) · 4.47 KB
/
integration_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"trackpump/domain/model"
"trackpump/usecase"
"trackpump/usecase/exception"
"github.com/labstack/echo"
)
func TestCreateAccount(t *testing.T) {
registry := NewRegistry(nil, nil, "EMAIL", "PASSWORD")
controller := registry.NewAppController()
inputUseCase := `{"name":"Aurelio Buarque", "email":"abuarquemf@gmail.com", "password":"123455678", "gender":0, "birth":"1997-11-29", "height":172}`
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(inputUseCase))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, rec)
controller.Create(c)
outputUseCase := usecase.CreateAccountOutput{}
if err := json.Unmarshal(rec.Body.Bytes(), &outputUseCase); err != nil {
t.Errorf("expected error nil when unmarshaling response, erro %q", err)
}
if outputUseCase.Email != "abuarquemf@gmail.com" {
t.Errorf("expected email to be abuarquemf@gmail.com, got %s", outputUseCase.Email)
}
if outputUseCase.Name != "Aurelio Buarque" {
t.Errorf("expected email to be Aurelio Buarque, got %s", outputUseCase.Name)
}
if outputUseCase.ID == "" {
t.Errorf("expected to have id different from an empty string")
}
}
func TestCreateAccountWithEmailAlreadyOnDB(t *testing.T) {
registry := NewRegistry(nil, nil, "EMAIL", "PASSWORD")
controller := registry.NewAppController()
registry.getRepository().Save(&model.User{
Email: "abuarquemf@gmail.com",
})
inputUseCase := `{"name":"Aurelio Buarque", "email":"abuarquemf@gmail.com", "password":"123455678", "gender":0, "birth":"1997-11-29", "height":172}`
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(inputUseCase))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, rec)
controller.Create(c)
errResponse := exception.Error{}
if err := json.Unmarshal(rec.Body.Bytes(), &errResponse); err != nil {
t.Errorf("expected error nil when unmarshaling response, erro %q", err)
}
if errResponse.Message != "email abuarquemf@gmail.com already in use" {
t.Errorf("expected to get \"email abuarquemf@gmail.com already in use\", got %s", errResponse.Message)
}
if errResponse.Code != 409 {
t.Errorf("expected code to be 409, got %d", errResponse.Code)
}
}
func TestLoginWithEmailNotPresentOnDB(t *testing.T) {
registry := NewRegistry(nil, nil, "EMAIL", "PASSWORD")
controller := registry.NewAppController()
inputUseCase := `{"email":"abuarquemf@gmail.com", "password":"12345678"}`
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(inputUseCase))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, rec)
controller.Login(c)
errResponse := exception.Error{}
if err := json.Unmarshal(rec.Body.Bytes(), &errResponse); err != nil {
t.Errorf("expected error nil when unmarshaling response, erro %q", err)
}
if errResponse.Message != "user not found with email abuarquemf@gmail.com " {
t.Errorf("expeted message \"user not found with email abuarquemf@gmail.com\", got %s", errResponse.Message)
}
if errResponse.Code != 404 {
t.Errorf("expected code 404, got %d", errResponse.Code)
}
}
func TestLoginWithEmailPresentOnDB(t *testing.T) {
registry := NewRegistry(nil, nil, "EMAIL", "PASSWORD")
controller := registry.NewAppController()
registry.getRepository().Save(&model.User{
Email: "abuarquemf@gmail.com",
Name: "Aurelio Buarque",
ID: "505",
Password: "$2a$10$X4m8N.KozNblKzHwm.2KpudOdq5k0TyNvFqBzo/G23eDgN4HKtyB6",
})
inputUseCase := `{"email":"abuarquemf@gmail.com", "password":"1234567"}`
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(inputUseCase))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, rec)
controller.Login(c)
outputUseCase := usecase.LoginOutput{}
if err := json.Unmarshal(rec.Body.Bytes(), &outputUseCase); err != nil {
t.Errorf("expected error nil when unmarshaling response, erro %q", err)
}
if outputUseCase.Email != "abuarquemf@gmail.com" {
t.Errorf("expected email abuarquemf@gmail.com, got %s", outputUseCase.Email)
}
if outputUseCase.Name != "Aurelio Buarque" {
t.Errorf("expected name Aurelio Buarque, got %s", outputUseCase.Name)
}
if outputUseCase.ID != "505" {
t.Errorf("exepcted ID 505, got %s", outputUseCase.ID)
}
}