forked from susana-garcia/gocloaksession
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_test.go
116 lines (81 loc) · 3.14 KB
/
session_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
package gocloaksession
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
)
const (
gocloakHostname = "http://localhost:8080"
gocloakRealm = "gocloak"
gocloakClientID = "gocloak"
gocloakClientSecret = "gocloak-secret"
)
func initializeSession(t testing.TB) *goCloakSession {
session, err := NewSession(gocloakClientID, gocloakClientSecret, gocloakRealm, gocloakHostname)
if err != nil {
t.Log(err)
t.FailNow()
}
return session.(*goCloakSession)
}
func Test_GetKeycloakAuthToken_Authentication(t *testing.T) {
session := initializeSession(t)
token, err := session.GetKeycloakAuthToken()
assert.NoError(t, err, "Login failed")
assert.NotZero(t, token.AccessToken, "Token is not set")
}
func Test_GetKeycloakAuthToken_StillValid(t *testing.T) {
session := initializeSession(t)
_ = session.authenticate()
require.NotNil(t, session.token, "Token is not set")
require.NotZero(t, session.token.AccessToken, "Token is not set")
require.NotZero(t, session.token.RefreshToken, "Token is not set")
oldToken := session.token.AccessToken
token, err := session.GetKeycloakAuthToken()
assert.NoError(t, err, "refreshToken failed")
assert.Equal(t, oldToken, token.AccessToken, "New AccessToken given, but expecting the old is still valid")
}
func Test_GetKeycloakAuthToken_Refresh(t *testing.T) {
session := initializeSession(t)
_ = session.authenticate()
require.NotNil(t, session.token, "Token is not set")
require.NotZero(t, session.token.AccessToken, "Token is not set")
require.NotZero(t, session.token.RefreshToken, "Token is not set")
oldToken := session.token.AccessToken
session.token.AccessToken = ""
token, err := session.GetKeycloakAuthToken()
assert.NoError(t, err, "refreshToken failed")
assert.NotEqual(t, oldToken, token.AccessToken, "No new AccessToken given")
}
func Test_refreshToken(t *testing.T) {
session := initializeSession(t)
_ = session.authenticate()
require.NotNil(t, session.token, "Token is not set")
require.NotZero(t, session.token.AccessToken, "Token is not set")
require.NotZero(t, session.token.RefreshToken, "Token is not set")
oldToken := session.token.AccessToken
err := session.refreshToken()
assert.NoError(t, err, "refreshToken failed")
assert.NotEqual(t, oldToken, session.token.AccessToken, "No new AccessToken given")
}
func Test_authenticate(t *testing.T) {
session := initializeSession(t)
err := session.authenticate()
assert.NoError(t, err, "authenticate failed")
assert.NotZero(t, session.token.AccessToken, "Token is not set")
}
func Test_AddAuthTokenToGRPCContext(t *testing.T) {
session := initializeSession(t)
_ = session.authenticate()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, err := session.AddAuthTokenToGRPCContext(ctx)
assert.NoError(t, err, "addAuthTokenToGRPCContext failed")
auth, ok := metadata.FromOutgoingContext(ctx)
assert.True(t, ok, "FromOutgoingContext failed")
assert.Contains(t, auth, "authorization", "authorization was not found")
assert.Contains(t, auth["authorization"][0], "Bearer ", "authorization is not set")
}