-
Notifications
You must be signed in to change notification settings - Fork 9
/
session_test.go
114 lines (79 loc) · 3.15 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
package gocloaksession_test
import (
"testing"
"github.com/Clarilab/gocloaksession"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
gocloakHostname = "http://localhost:8080"
gocloakRealm = "gocloak"
gocloakClientID = "gocloak"
gocloakClientSecret = "gocloak-secret"
)
func initializeSession(t testing.TB) gocloaksession.GoCloakSession {
session, err := gocloaksession.NewSession(gocloakClientID, gocloakClientSecret, gocloakRealm, gocloakHostname)
if err != nil {
t.Log(err)
t.FailNow()
}
return session
}
func Test_Integration_GetKeycloakAuthToken_Authentication(t *testing.T) {
t.Parallel()
session := initializeSession(t)
token, err := session.GetKeycloakAuthToken()
require.NoError(t, err, "Login failed")
assert.NotZero(t, token.AccessToken, "Token is not set")
}
func Test_Integration_GetKeycloakAuthToken_StillValid(t *testing.T) {
t.Parallel()
session := initializeSession(t)
_ = session.ForceAuthenticate()
oldToken, err := session.GetKeycloakAuthToken()
require.NoError(t, err, "failed to retrieve old token")
require.NotNil(t, oldToken, "Token is not set")
require.NotZero(t, oldToken.AccessToken, "AccessToken is not set")
require.NotZero(t, oldToken.RefreshToken, "RefreshToken is not set")
newToken, err := session.GetKeycloakAuthToken()
assert.NoError(t, err, "failed to retrieve new token")
assert.Equal(t, oldToken.AccessToken, newToken.AccessToken, "New AccessToken given, but expecting the old is still valid")
}
func Test_Integration_GetKeycloakAuthToken_Refresh(t *testing.T) {
t.Parallel()
session := initializeSession(t)
_ = session.ForceAuthenticate()
oldToken, err := session.GetKeycloakAuthToken()
require.NoError(t, err, "Failed to retrieve token")
require.NotNil(t, oldToken, "Token is not set")
require.NotZero(t, oldToken.AccessToken, "Token is not set")
require.NotZero(t, oldToken.RefreshToken, "Token is not set")
oldToken.AccessToken = ""
newToken, err := session.GetKeycloakAuthToken()
assert.NoError(t, err, "failed to retrieve token")
assert.NotEqual(t, oldToken.AccessToken, newToken.AccessToken, "No new AccessToken given")
}
func Test_Integration_refreshToken(t *testing.T) {
t.Parallel()
session := initializeSession(t)
_ = session.ForceAuthenticate()
oldToken, err := session.GetKeycloakAuthToken()
require.NoError(t, err, "Failed to retrieve token")
require.NotNil(t, oldToken, "Token is not set")
require.NotZero(t, oldToken.AccessToken, "Token is not set")
require.NotZero(t, oldToken.RefreshToken, "Token is not set")
err = session.ForceRefresh()
require.NoError(t, err, "Failed to refresh token")
newToken, err := session.GetKeycloakAuthToken()
require.NoError(t, err, "Failed to retrieve token")
assert.NotEqual(t, oldToken.AccessToken, newToken.AccessToken, "No new AccessToken given")
}
func Test_Integration_authenticate(t *testing.T) {
t.Parallel()
session := initializeSession(t)
err := session.ForceAuthenticate()
assert.NoError(t, err, "authenticate failed")
token, err := session.GetKeycloakAuthToken()
require.NoError(t, err, "Failed to retrieve token")
assert.NotZero(t, token.AccessToken, "Token is not set")
}