-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_test.go
123 lines (93 loc) · 2.89 KB
/
rest_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
118
119
120
121
122
123
package goautowp
import (
"context"
"testing"
"github.com/autowp/goautowp/config"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
)
const tokenWithInvalidSignature = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9." +
"eyJhdWQiOiJkZWZhdWx0Iiwic3ViIjoiMSJ9." +
"yuzUurjlDfEKchYseIrHQ1D5_RWnSuMxM-iK9FDNlQBBw8kCz3H-94xHvyd9pAA6Ry2-YkGi1v6Y3AHIpkDpcQ"
func TestGetVehicleTypesInaccessibleAnonymously(t *testing.T) {
t.Parallel()
srv, err := cnt.GRPCServer()
require.NoError(t, err)
_, err = srv.GetVehicleTypes(context.Background(), &emptypb.Empty{})
require.Error(t, err)
}
func TestGetVehicleTypesInaccessibleWithEmptyToken(t *testing.T) {
t.Parallel()
srv, err := cnt.GRPCServer()
require.NoError(t, err)
ctx := metadata.NewIncomingContext(
context.Background(),
metadata.New(map[string]string{authorizationHeader: bearerPrefix}),
)
_, err = srv.GetVehicleTypes(ctx, &emptypb.Empty{})
require.Error(t, err)
}
func TestGetVehicleTypesInaccessibleWithInvalidToken(t *testing.T) {
t.Parallel()
srv, err := cnt.GRPCServer()
require.NoError(t, err)
ctx := metadata.NewIncomingContext(
context.Background(),
metadata.New(map[string]string{authorizationHeader: bearerPrefix + "abc"}),
)
_, err = srv.GetVehicleTypes(ctx, &emptypb.Empty{})
require.Error(t, err)
}
func TestGetVehicleTypesInaccessibleWithWronglySignedToken(t *testing.T) {
t.Parallel()
srv, err := cnt.GRPCServer()
require.NoError(t, err)
ctx := metadata.NewIncomingContext(
context.Background(),
metadata.New(map[string]string{
authorizationHeader: bearerPrefix + tokenWithInvalidSignature,
}),
)
_, err = srv.GetVehicleTypes(ctx, &emptypb.Empty{})
require.Error(t, err)
}
func TestGetVehicleTypesInaccessibleWithoutModeratePrivilege(t *testing.T) {
t.Parallel()
cfg := config.LoadConfig(".")
ctx := context.Background()
kc := cnt.Keycloak()
token, err := kc.Login(ctx, "frontend", "", cfg.Keycloak.Realm, testUsername, testPassword)
require.NoError(t, err)
require.NotNil(t, token)
srv, err := cnt.GRPCServer()
require.NoError(t, err)
_, err = srv.GetVehicleTypes(
metadata.NewIncomingContext(
ctx,
metadata.New(map[string]string{authorizationHeader: bearerPrefix + token.AccessToken}),
),
&emptypb.Empty{},
)
require.Error(t, err)
}
func TestGetVehicleTypes(t *testing.T) {
t.Parallel()
cfg := config.LoadConfig(".")
ctx := context.Background()
kc := cnt.Keycloak()
token, err := kc.Login(ctx, "frontend", "", cfg.Keycloak.Realm, adminUsername, adminPassword)
require.NoError(t, err)
require.NotNil(t, token)
srv, err := cnt.GRPCServer()
require.NoError(t, err)
result, err := srv.GetVehicleTypes(
metadata.NewIncomingContext(
ctx,
metadata.New(map[string]string{authorizationHeader: bearerPrefix + token.AccessToken}),
),
&emptypb.Empty{},
)
require.NoError(t, err)
require.NotEmpty(t, result.GetItems())
}