-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_test.go
151 lines (135 loc) · 3.46 KB
/
api_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package fedex
import (
"errors"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
response_errors "github.com/RomaBilka/parcel-tracking/pkg/response-errors"
"github.com/stretchr/testify/assert"
)
func TestApi_TrackByTrackingNumber(t *testing.T) {
fedex := NewApi("", "", "", "")
fedex.token.token = "test_token"
fedex.token.expire = time.Now().Local().Add(60 * time.Minute)
testCases := []struct {
name string
file string
trackNumber string
status int
err error
}{
{
name: "Tracked bu number",
file: "fixtures/tracked_bu_number.json",
status: http.StatusOK,
trackNumber: "123456789012",
},
{
name: "Bad requesr",
file: "fixtures/bad_request_error.json",
status: http.StatusOK,
err: errors.New("The given JWT is invalid. Please modify your request and try again."),
},
{
name: "Tracking tracking number notfound",
file: "fixtures/tracking_trackingnumber_notfound.json",
status: http.StatusOK,
err: response_errors.NotFound,
},
{
name: "Validation errcd wspod",
file: "fixtures/validation_errcd_wspod.json",
status: http.StatusOK,
err: errors.New("We are unable to process this shipment for the moment. Try again later or contact FedEx Customer Service."),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(testCase.status)
b, err := os.ReadFile(testCase.file)
if err != nil {
t.Fatal(err)
}
_, err = w.Write(b)
assert.NoError(t, err)
}))
defer server.Close()
fedex.apiURL = server.URL
res, err := fedex.TrackByTrackingNumber(TrackingRequest{})
assert.Equal(t, testCase.err, err)
if res != nil {
assert.Equal(t, testCase.trackNumber, res.Output.CompleteTrackResults[0].TrackingNumber)
}
})
}
}
func TestApi_authorize(t *testing.T) {
fedex := NewApi("", "", "", "")
testCases := []struct {
name string
file string
token string
err error
}{
{
name: "Not authorized",
file: "fixtures/not_authorized_error.json",
token: "",
err: errors.New("Access token expired. Please modify your request and try again."),
},
{
name: "Short_lifetime",
file: "fixtures/token_with_short_lifetime.json",
token: "",
err: errors.New("short expiration of the token"),
},
{
name: "Ok",
token: "test token",
file: "fixtures/authorize_ok.json",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := os.ReadFile(testCase.file)
if err != nil {
t.Fatal(err)
}
_, err = w.Write(b)
assert.NoError(t, err)
}))
defer server.Close()
fedex.apiURL = server.URL
token, err := fedex.authorize()
assert.Equal(t, testCase.err, err)
assert.Equal(t, testCase.token, token)
})
}
}
func TestApi_isExpired(t *testing.T) {
testCases := []struct {
name string
expire time.Time
ok bool
}{
{
name: "Ok",
expire: time.Now().Local().Add(2 * time.Minute),
ok: false,
},
{
name: "expired",
expire: time.Now().Local().Add(500 * time.Millisecond),
ok: true,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.ok, isExpired(testCase.expire))
})
}
}