-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
111 lines (83 loc) · 2.38 KB
/
service_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
package schedder_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"gitlab.com/vlad.anghel/schedder-api"
)
func TestCreateService(t *testing.T) {
t.Parallel()
api := BeginTx(t)
defer api.Rollback()
email := "example@example.com"
password := "hackmenow"
tenantName := "Zâna Măseluță"
accountID := api.registerUserByEmail(email, password)
api.activateUserByEmail(email)
api.forceBusiness(email, true)
token := api.generateToken(email, password)
tenantID := api.createTenant(token, tenantName)
serviceName := "control_rutina"
request := schedder.CreateServiceRequest{
ServiceName: serviceName,
Price: 4.20,
Duration: 1 * time.Hour,
}
endpoint := fmt.Sprintf("/tenants/%s/personnel/%s/services", tenantID, accountID)
req, err := NewJSONRequest(http.MethodPost, endpoint, request)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Authorization", "Bearer " + token)
w := httptest.NewRecorder()
api.ServeHTTP(w, req)
resp := w.Result()
var response schedder.CreateServiceResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
t.Fatal(err)
}
expect(t, "", response.Error)
expect(t, http.StatusCreated, resp.StatusCode)
}
func TestServicesForPersonnel(t *testing.T) {
t.Parallel()
api := BeginTx(t)
defer api.Rollback()
email := "example@example.com"
password := "hackmenow"
tenantName := "Zâna Măseluță"
accountID := api.registerUserByEmail(email, password)
api.activateUserByEmail(email)
api.forceBusiness(email, true)
token := api.generateToken(email, password)
tenantID := api.createTenant(token, tenantName)
serviceID := api.createService(token, tenantID, accountID, "service1", 4.20, 1 * time.Hour)
endpoint := fmt.Sprintf("/tenants/%s/personnel/%s/services", tenantID, accountID)
req := httptest.NewRequest(http.MethodGet, endpoint, nil)
w := httptest.NewRecorder()
api.ServeHTTP(w, req)
resp := w.Result()
expect(t, http.StatusOK, resp.StatusCode)
var response schedder.ServicesResponse
err := json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
t.Fatal(err)
}
expect(t, "", response.Error)
found := false
for _, service := range response.Services {
if service.PersonnelID != accountID {
t.Fatal("service wasn't for this personnal")
}
if service.ServiceID == serviceID {
found = true
}
}
if !found {
t.Fatal("couldn't find service ", serviceID)
}
}