-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtoken_test.go
44 lines (34 loc) · 1019 Bytes
/
token_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
package metadata
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type TokenMockclient struct {
Token string
GenerateTokenErr error
}
func (m *TokenMockclient) GenerateToken(ctx context.Context, opts ...TokenOption) (string, error) {
if m.GenerateTokenErr != nil {
return "", m.GenerateTokenErr
}
return m.Token, nil
}
func TestGenerateToken_Success(t *testing.T) {
mockClient := &TokenMockclient{
Token: "mock-token-value",
}
token, err := mockClient.GenerateToken(context.Background())
assert.NoError(t, err, "Expected no error")
assert.Equal(t, "mock-token-value", token, "Unexpected token")
}
func TestGenerateToken_Error(t *testing.T) {
mockClient := &TokenMockclient{
GenerateTokenErr: errors.New("mock error"),
}
token, err := mockClient.GenerateToken(context.Background())
assert.Error(t, err, "Expected an error")
assert.Equal(t, "", token, "Expected empty token")
assert.EqualError(t, err, "mock error", "Unexpected error message")
}