|
1
|
|
|
package smobilpay |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"crypto/hmac" |
|
6
|
|
|
"crypto/sha1" |
|
7
|
|
|
"encoding/base64" |
|
8
|
|
|
"net/http" |
|
9
|
|
|
"net/url" |
|
10
|
|
|
"testing" |
|
11
|
|
|
"time" |
|
12
|
|
|
|
|
13
|
|
|
"github.com/NdoleStudio/smobilpay-go/internal/helpers" |
|
14
|
|
|
"github.com/NdoleStudio/smobilpay-go/internal/stubs" |
|
15
|
|
|
"github.com/stretchr/testify/assert" |
|
16
|
|
|
) |
|
17
|
|
|
|
|
18
|
|
|
func TestClient_Ping_Ok(t *testing.T) { |
|
19
|
|
|
// Setup |
|
20
|
|
|
t.Parallel() |
|
21
|
|
|
|
|
22
|
|
|
// Arrange |
|
23
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.PingOk()) |
|
24
|
|
|
accessToken := "6B352110-4716-11ED-963F-0800200C9A66" |
|
25
|
|
|
client := New( |
|
26
|
|
|
WithBaseURL(server.URL), |
|
27
|
|
|
WithAccessToken(accessToken), |
|
28
|
|
|
WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"), |
|
29
|
|
|
) |
|
30
|
|
|
nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995" |
|
31
|
|
|
|
|
32
|
|
|
// Act |
|
33
|
|
|
status, response, err := client.Ping( |
|
34
|
|
|
context.Background(), |
|
35
|
|
|
WithRequestTimestamp(time.Now()), |
|
36
|
|
|
WithRequestNonce(nonce), |
|
37
|
|
|
) |
|
38
|
|
|
|
|
39
|
|
|
// Assert |
|
40
|
|
|
assert.Nil(t, err) |
|
41
|
|
|
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
|
42
|
|
|
assert.Equal(t, nonce, status.Nonce) |
|
43
|
|
|
assert.Equal(t, accessToken, status.Key) |
|
44
|
|
|
|
|
45
|
|
|
// Teardown |
|
46
|
|
|
server.Close() |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
func TestClient_Ping_Error(t *testing.T) { |
|
50
|
|
|
// Setup |
|
51
|
|
|
t.Parallel() |
|
52
|
|
|
|
|
53
|
|
|
// Arrange |
|
54
|
|
|
server := helpers.MakeTestServer(http.StatusBadRequest, stubs.PingError()) |
|
55
|
|
|
accessToken := "6B352110-4716-11ED-963F-0800200C9A66" |
|
56
|
|
|
client := New( |
|
57
|
|
|
WithBaseURL(server.URL), |
|
58
|
|
|
WithAccessToken(accessToken), |
|
59
|
|
|
WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"), |
|
60
|
|
|
) |
|
61
|
|
|
nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995" |
|
62
|
|
|
|
|
63
|
|
|
// Act |
|
64
|
|
|
status, response, err := client.Ping( |
|
65
|
|
|
context.Background(), |
|
66
|
|
|
WithRequestTimestamp(time.Now()), |
|
67
|
|
|
WithRequestNonce(nonce), |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
// Assert |
|
71
|
|
|
assert.NotNil(t, err) |
|
72
|
|
|
assert.Nil(t, status) |
|
73
|
|
|
assert.Equal(t, http.StatusBadRequest, response.HTTPResponse.StatusCode) |
|
74
|
|
|
|
|
75
|
|
|
// Teardown |
|
76
|
|
|
server.Close() |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
func TestClient_Ping_Request(t *testing.T) { |
|
80
|
|
|
// Setup |
|
81
|
|
|
t.Parallel() |
|
82
|
|
|
|
|
83
|
|
|
// Arrange |
|
84
|
|
|
request := new(http.Request) |
|
85
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.PingOk(), request) |
|
86
|
|
|
accessToken := "6B352110-4716-11ED-963F-0800200C9A66" |
|
87
|
|
|
accessSecret := "1B875FB0-4717-11ED-963F-0800200C9A66" |
|
88
|
|
|
client := New( |
|
89
|
|
|
WithBaseURL(server.URL), |
|
90
|
|
|
WithAccessToken(accessToken), |
|
91
|
|
|
WithAccessSecret(accessSecret), |
|
92
|
|
|
) |
|
93
|
|
|
nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995" |
|
94
|
|
|
signature := computeHmac( |
|
95
|
|
|
"GET&"+url.QueryEscape(server.URL)+"%2Fping&s3pAuth_nonce%3D95cdf110-4614-4d95-b6c2-f14fe01c4995%26s3pAuth_signature_method%3DHMAC-SHA1%26s3pAuth_timestamp%3D1613869830%26s3pAuth_token%3D6B352110-4716-11ED-963F-0800200C9A66", |
|
96
|
|
|
accessSecret, |
|
97
|
|
|
) |
|
98
|
|
|
|
|
99
|
|
|
// Act |
|
100
|
|
|
_, _, err := client.Ping( |
|
101
|
|
|
context.Background(), |
|
102
|
|
|
WithRequestTimestamp(time.Date(2021, time.Month(2), 21, 1, 10, 30, 0, time.UTC)), |
|
103
|
|
|
WithRequestNonce(nonce), |
|
104
|
|
|
) |
|
105
|
|
|
|
|
106
|
|
|
// Assert |
|
107
|
|
|
assert.Nil(t, err) |
|
108
|
|
|
assert.Equal( |
|
109
|
|
|
t, |
|
110
|
|
|
"s3pAuth,s3pAuth_nonce=\"95cdf110-4614-4d95-b6c2-f14fe01c4995\",s3pAuth_signature=\""+signature+"\",s3pAuth_signature_method=\"HMAC-SHA1\",s3pAuth_timestamp=\"1613869830\",s3pAuth_token=\"6B352110-4716-11ED-963F-0800200C9A66\"", |
|
111
|
|
|
request.Header.Get("Authorization"), |
|
112
|
|
|
) |
|
113
|
|
|
|
|
114
|
|
|
// Teardown |
|
115
|
|
|
server.Close() |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
func computeHmac(message string, secret string) string { |
|
119
|
|
|
key := []byte(secret) |
|
120
|
|
|
h := hmac.New(sha1.New, key) |
|
121
|
|
|
_, _ = h.Write([]byte(message)) |
|
122
|
|
|
return base64.StdEncoding.EncodeToString(h.Sum(nil)) |
|
123
|
|
|
} |
|
124
|
|
|
|