-
Notifications
You must be signed in to change notification settings - Fork 6
/
offers_test.go
133 lines (110 loc) · 3.71 KB
/
offers_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2021-present Airheart, Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package duffel
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestListOffers(t *testing.T) {
defer gock.Off()
a := assert.New(t)
gock.New("https://api.duffel.com").
Get("/air/offers").
MatchParam("offer_request_id", "orq_00009htyDGjIfajdNBZRlw").
MatchParam("sort", "total_amount").
MatchParam("max_connections", "1").
Reply(200).
SetHeader("Ratelimit-Limit", "5").
SetHeader("Ratelimit-Remaining", "5").
SetHeader("Ratelimit-Reset", time.Now().Format(time.RFC1123)).
SetHeader("Date", time.Now().Format(time.RFC1123)).
File("fixtures/200-offers-orq_0000AGqEDX9VCvWmHLBywi.json")
ctx := context.TODO()
client := New("duffel_test_123")
iter := client.ListOffers(ctx, "orq_00009htyDGjIfajdNBZRlw", ListOffersParams{
Sort: ListOffersSortTotalAmount,
MaxConnections: 1,
})
iter.Next()
data := iter.Current()
err := iter.Err()
a.NoError(err)
a.NotNil(data)
a.Equal("228.60 USD", data.TotalAmount().String())
a.Len(data.Slices, 1)
}
func TestGetOfferByID(t *testing.T) {
defer gock.Off()
a := assert.New(t)
gock.New("https://api.duffel.com").
Get("/air/offers/off_00009htYpSCXrwaB9DnUm0").
MatchParam("return_available_services", "true").
Reply(200).
SetHeader("Ratelimit-Limit", "5").
SetHeader("Ratelimit-Remaining", "5").
SetHeader("Ratelimit-Reset", time.Now().Format(time.RFC1123)).
SetHeader("Date", time.Now().Format(time.RFC1123)).
File("fixtures/200-offers-off_00009htYpSCXrwaB9DnUm0.json")
ctx := context.TODO()
client := New("duffel_test_123")
data, err := client.GetOffer(ctx, "off_00009htYpSCXrwaB9DnUm0", GetOfferParams{
ReturnAvailableServices: true,
})
a.NoError(err)
a.NotNil(data)
a.Equal("45.00 GBP", data.TotalAmount().String())
a.Len(data.Slices, 1)
a.False(data.PaymentRequirements.RequiresInstantPayment)
a.Equal(time.Date(2020, 1, 17, 10, 42, 14, 0, time.UTC).Unix(), time.Time(*data.PaymentRequirements.PriceGuaranteeExpiresAt).Unix())
a.Equal(time.Date(2020, 1, 17, 10, 42, 14, 0, time.UTC).Unix(), time.Time(*data.PaymentRequirements.PaymentRequiredBy).Unix())
}
func TestUpdateOffserPassenger(t *testing.T) {
defer gock.Off()
// gock.Observe(gock.DumpRequest)
a := assert.New(t)
gock.New("https://api.duffel.com").
Patch("/air/offers/orq_123/passengers/pas_00009hj8USM7Ncg31cBCL").
Reply(200).
SetHeader("Ratelimit-Limit", "5").
SetHeader("Ratelimit-Remaining", "5").
SetHeader("Ratelimit-Reset", time.Now().Format(time.RFC1123)).
SetHeader("Date", time.Now().Format(time.RFC1123)).
File("fixtures/200-update-offer-passenger.json")
ctx := context.TODO()
client := New("duffel_test_123")
data, err := client.UpdateOfferPassenger(ctx, "orq_123", "pas_00009hj8USM7Ncg31cBCL", PassengerUpdateInput{
FamilyName: "Earhardt",
GivenName: "Amelia",
LoyaltyProgrammeAccounts: []LoyaltyProgrammeAccount{
{
AirlineIATACode: "AA",
AccountNumber: "AA1234567",
},
},
})
a.NoError(err)
a.NotNil(data)
a.Equal("pas_00009hj8USM7Ncg31cBCL", data.ID)
a.Equal("Earhardt", data.FamilyName)
a.Equal("Amelia", data.GivenName)
a.Equal("adult", data.Type.String())
a.Len(data.LoyaltyProgrammeAccounts, 1)
}
func TestListOffers_InavlidID(t *testing.T) {
a := assert.New(t)
ctx := context.TODO()
client := New("duffel_test_123")
iter := client.ListOffers(ctx, "fake-id", ListOffersParams{
Sort: ListOffersSortTotalAmount,
MaxConnections: 1,
})
iter.Next()
data := iter.Current()
err := iter.Err()
a.EqualError(err, "offerRequestId should begin with orq_")
a.Nil(data)
}