Skip to content

Commit 223d553

Browse files
committed
api/donation: update test according to API change
This patch updates the tests according to the TapPay record API change. After 2021/7/29, the record API will mark `rec_trade_id` or `bank_transaction_id` as required field. If these fields are not provided in the request, the time filter should be configured.
1 parent 6130055 commit 223d553

File tree

1 file changed

+103
-21
lines changed

1 file changed

+103
-21
lines changed

tests/controller_donations_test.go

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tests
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"net/http"
89
"net/http/httptest"
@@ -1463,17 +1464,25 @@ func TestGetVerificationOfATransaction(t *testing.T) {
14631464

14641465
}
14651466

1466-
func TestQueryTappayServer(t *testing.T) {
1467-
type (
1468-
filter struct {
1469-
OrderNumber string `json:"order_number"`
1467+
type (
1468+
recordFilter struct {
1469+
OrderNumber string `json:"order_number"`
1470+
RecTradeID null.String `json:"rec_trade_id"`
1471+
BankTransactionID null.String `json:"bank_transaction_id"`
1472+
Time struct {
1473+
StartTime null.Int `json:"start_time"`
1474+
EndTime null.Int `json:"end_time"`
14701475
}
1476+
}
14711477

1472-
requestBody struct {
1473-
RecordsPerPage uint `json:"records_per_page"`
1474-
Filters filter `json:"filters"`
1475-
}
1478+
recordRequestBody struct {
1479+
RecordsPerPage uint `json:"records_per_page"`
1480+
Filters recordFilter `json:"filters"`
1481+
}
1482+
)
14761483

1484+
func TestQueryTappayServer(t *testing.T) {
1485+
type (
14771486
tradeRecord struct {
14781487
RecordStatus int `json:"record_status"`
14791488
}
@@ -1540,10 +1549,15 @@ func TestQueryTappayServer(t *testing.T) {
15401549
Msg: "Gateway timeout",
15411550
}
15421551

1552+
queryMissingTimeRecord := tappayRecord{
1553+
Status: 537,
1554+
Msg: "Invalid arguments : filters > time > end_time",
1555+
}
1556+
15431557
cases := []struct {
15441558
reqHeader
15451559
name string
1546-
reqBody *requestBody
1560+
reqBody *recordRequestBody
15471561
preRecord *models.PayByPrimeDonation
15481562
stubTappayServer *httptest.Server
15491563
resultCode int
@@ -1562,8 +1576,8 @@ func TestQueryTappayServer(t *testing.T) {
15621576
Cookie: &maliciousCookie,
15631577
Authorization: maliciousAuthorization,
15641578
},
1565-
reqBody: &requestBody{
1566-
Filters: filter{
1579+
reqBody: &recordRequestBody{
1580+
Filters: recordFilter{
15671581
OrderNumber: "ValidOrderNumber1",
15681582
},
15691583
},
@@ -1576,29 +1590,61 @@ func TestQueryTappayServer(t *testing.T) {
15761590
Cookie: &cookie,
15771591
Authorization: authorization,
15781592
},
1579-
reqBody: &requestBody{
1580-
Filters: filter{
1593+
reqBody: &recordRequestBody{
1594+
Filters: recordFilter{
15811595
OrderNumber: "Invalid Order Number",
15821596
},
15831597
},
15841598
preRecord: &dbRecord,
15851599
resultCode: http.StatusNotFound,
15861600
},
1601+
{
1602+
name: "StatusCode=StatusOK,Query Success&Transaction Success&Provide required rec_trade_id or bank_transaction_id",
1603+
reqHeader: reqHeader{
1604+
Cookie: &cookie,
1605+
Authorization: authorization,
1606+
},
1607+
reqBody: &recordRequestBody{
1608+
Filters: recordFilter{
1609+
OrderNumber: "ValidOrderNumber1",
1610+
RecTradeID: null.StringFrom("ValidRecTradeID1"),
1611+
BankTransactionID: null.StringFrom("ValidBankTransactionID1"),
1612+
},
1613+
},
1614+
preRecord: &dbRecord,
1615+
stubTappayServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1616+
w.Header().Add("Content-Type", "application/json")
1617+
var resp []byte
1618+
if ValidateRecordRequest(t, r.Body) {
1619+
resp, _ = json.Marshal(transactionSuccessRecord)
1620+
} else {
1621+
resp, _ = json.Marshal(queryMissingTimeRecord)
1622+
}
1623+
w.Write(resp)
1624+
})),
1625+
resultCode: http.StatusOK,
1626+
resultCompare: &transactionSuccessRecord,
1627+
},
15871628
{
15881629
name: "StatusCode=StatusOK,Query Success&Transaction Success",
15891630
reqHeader: reqHeader{
15901631
Cookie: &cookie,
15911632
Authorization: authorization,
15921633
},
1593-
reqBody: &requestBody{
1594-
Filters: filter{
1634+
reqBody: &recordRequestBody{
1635+
Filters: recordFilter{
15951636
OrderNumber: "ValidOrderNumber1",
15961637
},
15971638
},
15981639
preRecord: &dbRecord,
15991640
stubTappayServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16001641
w.Header().Add("Content-Type", "application/json")
1601-
resp, _ := json.Marshal(transactionSuccessRecord)
1642+
var resp []byte
1643+
if ValidateRecordRequest(t, r.Body) {
1644+
resp, _ = json.Marshal(transactionSuccessRecord)
1645+
} else {
1646+
resp, _ = json.Marshal(queryMissingTimeRecord)
1647+
}
16021648
w.Write(resp)
16031649
})),
16041650
resultCode: http.StatusOK,
@@ -1610,15 +1656,20 @@ func TestQueryTappayServer(t *testing.T) {
16101656
Cookie: &cookie,
16111657
Authorization: authorization,
16121658
},
1613-
reqBody: &requestBody{
1614-
Filters: filter{
1659+
reqBody: &recordRequestBody{
1660+
Filters: recordFilter{
16151661
OrderNumber: "ValidOrderNumber1",
16161662
},
16171663
},
16181664
preRecord: &dbRecord,
16191665
stubTappayServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16201666
w.Header().Add("Content-Type", "application/json")
1621-
resp, _ := json.Marshal(transactionFailRecord)
1667+
var resp []byte
1668+
if ValidateRecordRequest(t, r.Body) {
1669+
resp, _ = json.Marshal(transactionFailRecord)
1670+
} else {
1671+
resp, _ = json.Marshal(queryMissingTimeRecord)
1672+
}
16221673
w.Write(resp)
16231674
})),
16241675
resultCode: http.StatusOK,
@@ -1631,8 +1682,8 @@ func TestQueryTappayServer(t *testing.T) {
16311682
Authorization: authorization,
16321683
},
16331684

1634-
reqBody: &requestBody{
1635-
Filters: filter{
1685+
reqBody: &recordRequestBody{
1686+
Filters: recordFilter{
16361687
OrderNumber: "ValidOrderNumber1",
16371688
},
16381689
},
@@ -1690,6 +1741,37 @@ func TestQueryTappayServer(t *testing.T) {
16901741
}
16911742
}
16921743

1744+
func ValidateRecordRequest(t *testing.T, body io.ReadCloser) bool {
1745+
t.Helper()
1746+
var err error
1747+
defer func() {
1748+
if err != nil {
1749+
t.Error(err)
1750+
}
1751+
}()
1752+
1753+
b, err := ioutil.ReadAll(body)
1754+
if err != nil {
1755+
return false
1756+
}
1757+
defer body.Close()
1758+
1759+
var rb recordRequestBody
1760+
err = json.Unmarshal(b, &rb)
1761+
if err != nil {
1762+
return false
1763+
}
1764+
1765+
// If the request filter does not provide either `rec_trade_id` or `bank_transaction_id` filter,
1766+
// the time filter should be set.
1767+
if rb.Filters.RecTradeID.IsZero() && rb.Filters.BankTransactionID.IsZero() {
1768+
if rb.Filters.Time.EndTime.IsZero() || rb.Filters.Time.StartTime.IsZero() {
1769+
return false
1770+
}
1771+
}
1772+
return true
1773+
}
1774+
16931775
/* GetDonationsOfAUser is not implemented yet
16941776
func TestGetDonations(t *testing.T) {
16951777
var resBody responseBodyForList

0 commit comments

Comments
 (0)