|
1
|
|
|
package campay |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"net/http" |
|
5
|
|
|
"testing" |
|
6
|
|
|
|
|
7
|
|
|
"github.com/stretchr/testify/assert" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
func TestNew(t *testing.T) { |
|
11
|
|
|
t.Run("default configuration is used when no option is set", func(t *testing.T) { |
|
12
|
|
|
// act |
|
13
|
|
|
client := New() |
|
14
|
|
|
|
|
15
|
|
|
// assert |
|
16
|
|
|
assert.NotEmpty(t, client.environment) |
|
17
|
|
|
assert.NotEmpty(t, client.common) |
|
18
|
|
|
|
|
19
|
|
|
assert.Empty(t, client.apiUsername) |
|
20
|
|
|
assert.Empty(t, client.apiPassword) |
|
21
|
|
|
|
|
22
|
|
|
assert.NotNil(t, client.httpClient) |
|
23
|
|
|
assert.NotNil(t, client.Transaction) |
|
24
|
|
|
}) |
|
25
|
|
|
|
|
26
|
|
|
t.Run("single configuration value can be set using options", func(t *testing.T) { |
|
27
|
|
|
// Arrange |
|
28
|
|
|
env := Environment("https://example.com") |
|
29
|
|
|
|
|
30
|
|
|
// Act |
|
31
|
|
|
client := New(WithEnvironment(env)) |
|
32
|
|
|
|
|
33
|
|
|
// Assert |
|
34
|
|
|
assert.NotNil(t, client.environment) |
|
35
|
|
|
assert.Equal(t, env.String(), client.environment.String()) |
|
36
|
|
|
}) |
|
37
|
|
|
|
|
38
|
|
|
t.Run("multiple configuration values can be set using options", func(t *testing.T) { |
|
39
|
|
|
// Arrange |
|
40
|
|
|
env := Environment("https://example.com") |
|
41
|
|
|
newHTTPClient := &http.Client{Timeout: 422} |
|
42
|
|
|
|
|
43
|
|
|
// Act |
|
44
|
|
|
client := New(WithEnvironment(env), WithHTTPClient(newHTTPClient)) |
|
45
|
|
|
|
|
46
|
|
|
// Assert |
|
47
|
|
|
assert.NotEmpty(t, client.environment) |
|
48
|
|
|
assert.Equal(t, env.String(), client.environment.String()) |
|
49
|
|
|
|
|
50
|
|
|
assert.NotNil(t, client.httpClient) |
|
51
|
|
|
assert.Equal(t, newHTTPClient.Timeout, client.httpClient.Timeout) |
|
52
|
|
|
}) |
|
53
|
|
|
|
|
54
|
|
|
t.Run("it sets the Transaction service correctly", func(t *testing.T) { |
|
55
|
|
|
// Arrange |
|
56
|
|
|
client := New() |
|
57
|
|
|
|
|
58
|
|
|
// Assert |
|
59
|
|
|
assert.NotNil(t, client.Transaction) |
|
60
|
|
|
assert.Equal(t, client.environment.String(), client.Transaction.client.environment.String()) |
|
61
|
|
|
}) |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
func TestClient_ValidateCallback(t *testing.T) { |
|
65
|
|
|
t.Run("valid signature and key returns nil error", func(t *testing.T) { |
|
66
|
|
|
// Arrange |
|
67
|
|
|
client := New() |
|
68
|
|
|
key := []byte("geTrvNBLvXS35UvK3PnTnQgpjGmaEGe7wa6k3Ns4zehhvjncsjXnQsV7ZzhDWjDMEt7") |
|
69
|
|
|
signature := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.9kFRoXMiiGWhCbnDPoOMVhgVTzYxu2MjNi-uTWGRcEU" |
|
70
|
|
|
|
|
71
|
|
|
// Act |
|
72
|
|
|
err := client.ValidateCallback(signature, key) |
|
73
|
|
|
|
|
74
|
|
|
// Assert |
|
75
|
|
|
assert.Nil(t, err) |
|
76
|
|
|
}) |
|
77
|
|
|
|
|
78
|
|
|
t.Run("invalid key returns and error", func(t *testing.T) { |
|
79
|
|
|
// Arrange |
|
80
|
|
|
client := New() |
|
81
|
|
|
key := []byte("geTrvNBLvXS35UvK3PnTnQgpjGmaEGe7wa6k3Ns4zehhvjncsjXnQsV7ZzhDWjDMEt7-invalid") |
|
82
|
|
|
signature := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.9kFRoXMiiGWhCbnDPoOMVhgVTzYxu2MjNi-uTWGRcEU" |
|
83
|
|
|
|
|
84
|
|
|
// Act |
|
85
|
|
|
err := client.ValidateCallback(signature, key) |
|
86
|
|
|
|
|
87
|
|
|
// Assert |
|
88
|
|
|
assert.NotNil(t, err) |
|
89
|
|
|
}) |
|
90
|
|
|
} |
|
91
|
|
|
|