Skip to content

Commit cc908d4

Browse files
committed
reorganize tests
1 parent 52d0660 commit cc908d4

File tree

5 files changed

+194
-156
lines changed

5 files changed

+194
-156
lines changed

identity/identity_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@ import (
99
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1010
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1111
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
12-
"github.com/redis-developer/go-redis-entraid/internal/mocks"
1312
"github.com/stretchr/testify/mock"
1413
)
1514

16-
var testJWTToken = mocks.TestJWTToken
15+
// testJWTToken is a JWT manager for testing
16+
//
17+
// {
18+
// "iss": "test jwt",
19+
// "iat": 1743515011,
20+
// "exp": 1775051011,
21+
// "aud": "www.example.com",
22+
// "sub": "[email protected]",
23+
// "oid": "test"
24+
// }
25+
//
26+
// key: qwertyuiopasdfghjklzxcvbnm123456
27+
const testJWTToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0IGp3dCIsImlhdCI6MTc0MzUxNTAxMSwiZXhwIjoxNzc1MDUxMDExLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwib2lkIjoidGVzdCJ9.6RG721V2eFlSLsCRmo53kSRRrTZIe1UPdLZCUEvIarU"
1728

1829
type mockAzureCredential struct {
1930
mock.Mock

internal/mocks/mocks.go

Lines changed: 0 additions & 145 deletions
This file was deleted.

internal/utils_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package internal
2+
3+
import "testing"
4+
5+
func TestIsClosedWithNilChannel(t *testing.T) {
6+
t.Parallel()
7+
var ch chan struct{}
8+
if IsClosed(ch) {
9+
t.Error("expected nil channel to be open")
10+
}
11+
}
12+
13+
func TestIsClosedWithEmptyChannel(t *testing.T) {
14+
t.Parallel()
15+
ch := make(chan struct{})
16+
if IsClosed(ch) {
17+
t.Error("expected empty channel to be open")
18+
}
19+
20+
close(ch)
21+
if !IsClosed(ch) {
22+
t.Error("expected empty channel to be closed")
23+
}
24+
}
25+
26+
func TestIsClosedWithClosedChannel(t *testing.T) {
27+
t.Parallel()
28+
ch := make(chan struct{})
29+
close(ch)
30+
if !IsClosed(ch) {
31+
t.Error("expected closed channel to be closed")
32+
}
33+
}

manager/manager_test.go

Lines changed: 139 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,148 @@
11
package manager
22

33
import (
4+
"net"
5+
"os"
6+
"time"
7+
48
"github.com/redis-developer/go-redis-entraid/internal"
5-
"github.com/redis-developer/go-redis-entraid/internal/mocks"
9+
"github.com/redis-developer/go-redis-entraid/shared"
10+
"github.com/redis-developer/go-redis-entraid/token"
11+
"github.com/stretchr/testify/mock"
612
)
713

8-
type mockIdentityProviderResponseParser = mocks.IdentityProviderResponseParser
9-
type mockIdentityProvider = mocks.IdentityProvider
10-
type mockTokenListener = mocks.TokenListener
14+
// testJWTToken is a JWT manager for testing
15+
//
16+
// {
17+
// "iss": "test jwt",
18+
// "iat": 1743515011,
19+
// "exp": 1775051011,
20+
// "aud": "www.example.com",
21+
// "sub": "[email protected]",
22+
// "oid": "test"
23+
// }
24+
//
25+
// key: qwertyuiopasdfghjklzxcvbnm123456
26+
const testJWTToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0IGp3dCIsImlhdCI6MTc0MzUxNTAxMSwiZXhwIjoxNzc1MDUxMDExLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwib2lkIjoidGVzdCJ9.6RG721V2eFlSLsCRmo53kSRRrTZIe1UPdLZCUEvIarU"
27+
28+
// testJWTExpiredToken is an expired JWT manager for testing
29+
//
30+
// {
31+
// "iss": "test jwt",
32+
// "iat": 1617795148,
33+
// "exp": 1617795148,
34+
// "aud": "www.example.com",
35+
// "sub": "[email protected]",
36+
// "oid": "test"
37+
// }
38+
//
39+
// key: qwertyuiopasdfghjklzxcvbnm123456
40+
const testJWTExpiredToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0IGp3dCIsImlhdCI6MTYxNzc5NTE0OCwiZXhwIjoxNjE3Nzk1MTQ4LCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwib2lkIjoidGVzdCJ9.IbGPhHRiPYcpUDrhAPf4h3gH1XXBOu560NYT59rUMzc"
41+
42+
// testJWTWithZeroExpiryToken is a JWT manager with zero expiry for testing
43+
//
44+
// {
45+
// "iss": "test jwt",
46+
// "iat": 1744025944,
47+
// "exp": null,
48+
// "aud": "www.example.com",
49+
// "sub": "[email protected]",
50+
// "oid": "test"
51+
// }
52+
// key: qwertyuiopasdfghjklzxcvbnm123456
53+
const testJWTWithZeroExpiryToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0IGp3dCIsImlhdCI6MTc0NDAyNTk0NCwiZXhwIjpudWxsLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwib2lkIjoidGVzdCJ9.bLSANIzawE5Y6rgspvvUaRhkBq6Y4E0ggjXlmHRn8ew"
54+
55+
var testTokenValid = token.New(
56+
"test",
57+
"password",
58+
"test",
59+
time.Now().Add(time.Hour),
60+
time.Now(),
61+
int64(time.Hour),
62+
)
63+
64+
type mockIdentityProviderResponseParser struct {
65+
// Mock implementation of the IdentityProviderResponseParser interface
66+
mock.Mock
67+
}
68+
69+
func (m *mockIdentityProviderResponseParser) ParseResponse(response shared.IdentityProviderResponse) (*token.Token, error) {
70+
args := m.Called(response)
71+
if args.Get(0) == nil {
72+
return nil, args.Error(1)
73+
}
74+
return args.Get(0).(*token.Token), args.Error(1)
75+
}
76+
77+
type mockIdentityProvider struct {
78+
// Mock implementation of the mockIdentityProvider interface
79+
// Add any necessary fields or methods for the mock identity provider here
80+
mock.Mock
81+
}
82+
83+
func (m *mockIdentityProvider) RequestToken() (shared.IdentityProviderResponse, error) {
84+
args := m.Called()
85+
if args.Get(0) == nil {
86+
return nil, args.Error(1)
87+
}
88+
return args.Get(0).(shared.IdentityProviderResponse), args.Error(1)
89+
}
90+
91+
func newMockError(retriable bool) error {
92+
if retriable {
93+
return &mockError{
94+
isTimeout: true,
95+
isTemporary: true,
96+
error: os.ErrDeadlineExceeded,
97+
}
98+
} else {
99+
return &mockError{
100+
isTimeout: false,
101+
isTemporary: false,
102+
error: os.ErrInvalid,
103+
}
104+
}
105+
}
106+
107+
type mockError struct {
108+
// Mock implementation of the network error
109+
error
110+
isTimeout bool
111+
isTemporary bool
112+
}
113+
114+
func (m *mockError) Error() string {
115+
return "this is mock error"
116+
}
117+
118+
func (m *mockError) Timeout() bool {
119+
return m.isTimeout
120+
}
121+
func (m *mockError) Temporary() bool {
122+
return m.isTemporary
123+
}
124+
func (m *mockError) Unwrap() error {
125+
return m.error
126+
}
127+
128+
func (m *mockError) Is(err error) bool {
129+
return m.error == err
130+
}
131+
132+
var _ net.Error = (*mockError)(nil)
133+
134+
type mockTokenListener struct {
135+
// Mock implementation of the TokenManagerListener interface
136+
mock.Mock
137+
Id int32
138+
}
139+
140+
func (m *mockTokenListener) OnTokenNext(token *token.Token) {
141+
_ = m.Called(token)
142+
}
11143

12-
var newMockError = mocks.NewError
13-
var testTokenValid = mocks.TestTokenValid
14-
var testJWTToken = mocks.TestJWTToken
15-
var testJWTExpiredToken = mocks.TestJWTExpiredToken
16-
var testJWTWithZeroExpiryToken = mocks.TestJWTWithZeroExpiryToken
144+
func (m *mockTokenListener) OnTokenError(err error) {
145+
_ = m.Called(err)
146+
}
17147

18148
type authResult = internal.IDPResp

0 commit comments

Comments
 (0)