forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_test.go
108 lines (86 loc) · 2.78 KB
/
component_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
package service
import (
"context"
"log"
"net/http"
"os"
"testing"
"time"
trainerHTTP "github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/client/trainer"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/genproto/trainer"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/server"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/tests"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/ports"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
func TestHoursAvailability(t *testing.T) {
t.Parallel()
token := tests.FakeTrainerJWT(t, uuid.New().String())
client := tests.NewTrainerHTTPClient(t, token)
hour := tests.RelativeDate(11, 12)
expectedHour := trainerHTTP.Hour{
Available: true,
HasTrainingScheduled: false,
Hour: hour,
}
date := hour.Truncate(24 * time.Hour)
from := date.AddDate(0, 0, -1)
to := date.AddDate(0, 0, 1)
getHours := func() []trainerHTTP.Hour {
dates := client.GetTrainerAvailableHours(t, from, to)
for _, d := range dates {
if d.Date.Equal(date) {
return d.Hours
}
}
t.Fatalf("Date not found in dates: %+v", dates)
return nil
}
client.MakeHourUnavailable(t, hour)
require.NotContains(t, getHours(), expectedHour)
code := client.MakeHourAvailable(t, hour)
require.Equal(t, http.StatusNoContent, code)
require.Contains(t, getHours(), expectedHour)
client.MakeHourUnavailable(t, hour)
require.NotContains(t, getHours(), expectedHour)
}
func TestUnauthorizedForAttendee(t *testing.T) {
t.Parallel()
token := tests.FakeAttendeeJWT(t, uuid.New().String())
client := tests.NewTrainerHTTPClient(t, token)
hour := tests.RelativeDate(11, 13)
code := client.MakeHourAvailable(t, hour)
require.Equal(t, http.StatusUnauthorized, code)
}
func startService() bool {
app := NewApplication(context.Background())
trainerHTTPAddr := os.Getenv("TRAINER_HTTP_ADDR")
go server.RunHTTPServerOnAddr(trainerHTTPAddr, func(router chi.Router) http.Handler {
return ports.HandlerFromMux(ports.NewHttpServer(app), router)
})
trainerGrpcAddr := os.Getenv("TRAINER_GRPC_ADDR")
go server.RunGRPCServerOnAddr(trainerGrpcAddr, func(server *grpc.Server) {
svc := ports.NewGrpcServer(app)
trainer.RegisterTrainerServiceServer(server, svc)
})
ok := tests.WaitForPort(trainerHTTPAddr)
if !ok {
log.Println("Timed out waiting for trainer HTTP to come up")
return false
}
ok = tests.WaitForPort(trainerGrpcAddr)
if !ok {
log.Println("Timed out waiting for trainer gRPC to come up")
}
return ok
}
func TestMain(m *testing.M) {
if !startService() {
log.Println("Timed out waiting for trainings HTTP to come up")
os.Exit(1)
}
os.Exit(m.Run())
}