-
Notifications
You must be signed in to change notification settings - Fork 0
/
clients.go
158 lines (129 loc) · 4.56 KB
/
clients.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package tests
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/dbaeka/workouts-go/internal/common/client/trainer"
"github.com/dbaeka/workouts-go/internal/common/client/trainings"
"github.com/dbaeka/workouts-go/internal/common/client/users"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func authorizationBearer(token string) func(context.Context, *http.Request) error {
return func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
return nil
}
}
type TrainerHTTPClient struct {
client *trainer.ClientWithResponses
}
func NewTrainerHTTPClient(t *testing.T, token string) TrainerHTTPClient {
url := fmt.Sprintf("http://%v/api", os.Getenv("TRAINER_HTTP_ADDR"))
client, err := trainer.NewClientWithResponses(
url,
trainer.WithRequestEditorFn(authorizationBearer(token)),
)
require.NoError(t, err)
return TrainerHTTPClient{
client: client,
}
}
func (c TrainerHTTPClient) MakeHourAvailable(t *testing.T, hour time.Time) int {
response, err := c.client.MakeHourAvailable(context.Background(), trainer.MakeHourAvailableJSONRequestBody{
Hours: []time.Time{hour},
})
require.NoError(t, err)
require.NoError(t, response.Body.Close())
return response.StatusCode
}
func (c TrainerHTTPClient) MakeHourUnavailable(t *testing.T, hour time.Time) {
response, err := c.client.MakeHourUnavailable(context.Background(), trainer.MakeHourUnavailableJSONRequestBody{
Hours: []time.Time{hour},
})
require.NoError(t, err)
require.NoError(t, response.Body.Close())
require.Equal(t, http.StatusNoContent, response.StatusCode)
}
func (c TrainerHTTPClient) GetTrainerAvailableHours(t *testing.T, from time.Time, to time.Time) []trainer.Date {
response, err := c.client.GetTrainerAvailableHoursWithResponse(context.Background(), &trainer.GetTrainerAvailableHoursParams{
DateFrom: from,
DateTo: to,
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
return *response.JSON200
}
type TrainingsHTTPClient struct {
client *trainings.ClientWithResponses
}
func NewTrainingsHTTPClient(t *testing.T, token string) TrainingsHTTPClient {
url := fmt.Sprintf("http://%v/api", os.Getenv("TRAININGS_HTTP_ADDR"))
client, err := trainings.NewClientWithResponses(
url,
trainings.WithRequestEditorFn(authorizationBearer(token)),
)
require.NoError(t, err)
return TrainingsHTTPClient{
client: client,
}
}
func (c TrainingsHTTPClient) CreateTraining(t *testing.T, note string, hour time.Time) string {
response, err := c.client.CreateTrainingWithResponse(context.Background(), trainings.CreateTrainingJSONRequestBody{
Notes: note,
Time: hour,
})
require.NoError(t, err)
require.Equal(t, http.StatusNoContent, response.StatusCode())
contentLocation := response.HTTPResponse.Header.Get("content-location")
return lastPathElement(contentLocation)
}
func lastPathElement(path string) string {
parts := strings.Split(path, "/")
return parts[len(parts)-1]
}
func (c TrainingsHTTPClient) CreateTrainingShouldFail(t *testing.T, note string, hour time.Time) {
response, err := c.client.CreateTraining(context.Background(), trainings.CreateTrainingJSONRequestBody{
Notes: note,
Time: hour,
})
require.NoError(t, err)
require.NoError(t, response.Body.Close())
require.Equal(t, http.StatusInternalServerError, response.StatusCode)
}
func (c TrainingsHTTPClient) GetTrainings(t *testing.T) trainings.Trainings {
response, err := c.client.GetTrainingsWithResponse(context.Background())
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
return *response.JSON200
}
func (c TrainingsHTTPClient) CancelTraining(t *testing.T, trainingUUID string, expectedStatusCode int) {
response, err := c.client.CancelTraining(context.Background(), uuid.MustParse(trainingUUID))
require.NoError(t, err)
require.NoError(t, response.Body.Close())
require.Equal(t, expectedStatusCode, response.StatusCode)
}
type UsersHTTPClient struct {
client *users.ClientWithResponses
}
func NewUsersHTTPClient(t *testing.T, token string) UsersHTTPClient {
url := fmt.Sprintf("http://%v/api", os.Getenv("USERS_HTTP_ADDR"))
client, err := users.NewClientWithResponses(
url,
users.WithRequestEditorFn(authorizationBearer(token)),
)
require.NoError(t, err)
return UsersHTTPClient{
client: client,
}
}
func (c UsersHTTPClient) GetCurrentUser(t *testing.T) users.User {
response, err := c.client.GetCurrentUserWithResponse(context.Background())
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
return *response.JSON200
}