This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
card_test.go
118 lines (100 loc) · 2.68 KB
/
card_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
115
116
117
118
package qiwi
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestCardRequest(t *testing.T) {
// Expected reply from QIWI
// HTTP/1.1 200 OK
// Content-Type: application/json
reply := `
{
"siteId": "test-01",
"billId": "gg",
"amount": {
"currency": "RUB",
"value": 42.24
},
"status": {
"value": "WAITING",
"changedDateTime": "2019-08-28T16:26:36.835+03:00"
},
"customFields": {},
"comment": "Spasibo",
"creationDateTime": "2019-08-28T16:26:36.835+03:00",
"expirationDateTime": "2019-09-13T14:30:00+03:00",
"payUrl": "https://oplata.qiwi.com/form/?invoice_uid=78d60ca9-7c99-481f-8e51-0100c9012087"
}`
errReply := `
{
"serviceName" : "payin-core",
"errorCode" : "validation.wrongmethod",
"description" : "Wrong method",
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}
`
serv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PUT":
var buf bytes.Buffer
var payload Payment
_, _ = io.Copy(&buf, r.Body)
_ = json.Unmarshal(buf.Bytes(), &payload)
isSale := false
for _, flag := range payload.Flags {
if flag == "SALE" {
isSale = true
break
}
}
if !isSale {
fmt.Println(w, errReply)
return
}
fmt.Fprintln(w, reply)
default:
fmt.Fprint(w, errReply)
}
}))
serv.Start()
defer serv.Close()
// Route request to mocked http server
pay := New("billId", "SiteID", "TOKEN", serv.URL)
err := pay.CardRequest(context.TODO(), 100)
if err != nil {
t.Errorf("CardRequest error: %s", err)
}
// dont need to pass this
// if pay.PaymentMethod.Type != CardPayment {
// t.Errorf("Wrong payment type %s", pay.PaymentMethod.Type)
// }
if pay.PayURL != "https://oplata.qiwi.com/form/?invoice_uid=78d60ca9-7c99-481f-8e51-0100c9012087" {
t.Error("PayURL not received")
}
}
func TestLocationTime(t *testing.T) {
pay := New("billID", "siteID", "token", "")
_ = pay.CardRequest(context.TODO(), 100)
// Moscow time
msktz, _ := time.LoadLocation("Europe/Moscow")
msktime := time.Now().In(msktz)
if pay.Expiration.Before(msktime) {
t.Error("Bad expiration time", pay.Expiration, msktime)
}
}
func TestPublicLink(t *testing.T) {
const wantLink = `https://oplata.qiwi.com/create?publicKey=pubKey&comment=some%20comment&amount=3.01`
payLink := PublicLink("pubKey", "some comment", 301)
if payLink != wantLink {
t.Errorf("Wrong payment link: %s, want %s", payLink, wantLink)
}
}