-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain_test.go
More file actions
305 lines (280 loc) · 8.14 KB
/
domain_test.go
File metadata and controls
305 lines (280 loc) · 8.14 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseAPIdate(t *testing.T) {
tests := []struct {
name string
dateString string
expected time.Time
expectError bool
}{
{
name: "valid date string",
dateString: "2023-03-15T14:30:45Z",
expected: time.Date(2023, 3, 15, 14, 30, 45, 0, time.UTC),
expectError: false,
},
{
name: "valid date string with zero time",
dateString: "2020-01-01T00:00:00Z",
expected: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expectError: false,
},
{
name: "valid date string end of year",
dateString: "2022-12-31T23:59:59Z",
expected: time.Date(2022, 12, 31, 23, 59, 59, 0, time.UTC),
expectError: false,
},
{
name: "invalid date format - missing Z",
dateString: "2023-03-15T14:30:45",
expected: time.Time{},
expectError: true,
},
{
name: "invalid date format - wrong separator",
dateString: "2023/03/15T14:30:45Z",
expected: time.Time{},
expectError: true,
},
{
name: "empty string",
dateString: "",
expected: time.Time{},
expectError: true,
},
{
name: "invalid month",
dateString: "2023-13-15T14:30:45Z",
expected: time.Time{},
expectError: true,
},
{
name: "invalid day",
dateString: "2023-02-30T14:30:45Z",
expected: time.Time{},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseAPIdate(tt.dateString)
if tt.expectError {
assert.Error(t, err, "Expected an error for input: %s", tt.dateString)
} else {
require.NoError(t, err, "Unexpected error for input: %s", tt.dateString)
assert.Equal(t, tt.expected, result, "Parsed date doesn't match expected for input: %s", tt.dateString)
}
})
}
}
func TestDomain_IsBelowCutoff(t *testing.T) {
cutoffDate := time.Date(2023, 6, 1, 0, 0, 0, 0, time.UTC)
tests := []struct {
name string
domain Domain
expected bool
}{
{
name: "domain with no delete date should be included",
domain: Domain{
Name: "example.com",
DeleteDateTime: "",
},
expected: true,
},
{
name: "domain deleted after cutoff should be included",
domain: Domain{
Name: "example.com",
DeleteDateTime: "2023-07-15T10:30:00Z", // After cutoff
},
expected: true,
},
{
name: "domain deleted on cutoff date should be excluded",
domain: Domain{
Name: "example.com",
DeleteDateTime: "2023-06-01T00:00:00Z", // Exactly on cutoff
},
expected: false,
},
{
name: "domain deleted before cutoff should be excluded",
domain: Domain{
Name: "example.com",
DeleteDateTime: "2023-05-15T10:30:00Z", // Before cutoff
},
expected: false,
},
{
name: "domain deleted just after cutoff should be included",
domain: Domain{
Name: "example.com",
DeleteDateTime: "2023-06-01T00:00:01Z", // 1 second after cutoff
},
expected: true,
},
{
name: "domain deleted just before cutoff should be excluded",
domain: Domain{
Name: "example.com",
DeleteDateTime: "2023-05-31T23:59:59Z", // 1 second before cutoff
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.domain.IsBelowCutoff(cutoffDate)
assert.Equal(t, tt.expected, result, "IsBelowCutoff result doesn't match expected for domain: %s", tt.domain.Name)
})
}
}
func TestDomain_IsBelowCutoff_EdgeCases(t *testing.T) {
// Test with different cutoff dates
t.Run("leap year cutoff", func(t *testing.T) {
cutoffDate := time.Date(2024, 2, 29, 12, 0, 0, 0, time.UTC) // Leap year
domain := Domain{
Name: "leap.com",
DeleteDateTime: "2024-03-01T00:00:00Z", // Day after leap day
}
result := domain.IsBelowCutoff(cutoffDate)
assert.True(t, result, "Domain deleted after leap year cutoff should be included")
})
t.Run("year boundary", func(t *testing.T) {
cutoffDate := time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC)
domain := Domain{
Name: "newyear.com",
DeleteDateTime: "2024-01-01T00:00:00Z", // New year
}
result := domain.IsBelowCutoff(cutoffDate)
assert.True(t, result, "Domain deleted in new year should be included")
})
}
// Benchmark tests to ensure performance is acceptable
func BenchmarkParseAPIdate(b *testing.B) {
dateString := "2023-03-15T14:30:45Z"
for i := 0; i < b.N; i++ {
_, _ = parseAPIdate(dateString)
}
}
func BenchmarkDomain_IsBelowCutoff(b *testing.B) {
cutoffDate := time.Date(2023, 6, 1, 0, 0, 0, 0, time.UTC)
domain := Domain{
Name: "benchmark.com",
DeleteDateTime: "2023-07-15T10:30:00Z",
}
for i := 0; i < b.N; i++ {
_ = domain.IsBelowCutoff(cutoffDate)
}
}
func TestFetchNicmanagerAPI(t *testing.T) {
tests := []struct {
name string
responseBody string
responseStatus int
expectedError bool
login string
password string
pageNo int
}{
{
name: "successful API call",
responseBody: `[{"name":"example.com","order_status":"active","order_datetime":"2023-01-01T00:00:00Z","registration_datetime":"2023-01-01T00:00:00Z","delete_datetime":""}]`,
responseStatus: 200,
expectedError: false,
login: "testuser",
password: "testpass",
pageNo: 1,
},
{
name: "empty response",
responseBody: `[]`,
responseStatus: 200,
expectedError: false,
login: "testuser",
password: "testpass",
pageNo: 1,
},
{
name: "unauthorized error",
responseBody: `{"error":"unauthorized"}`,
responseStatus: 401,
expectedError: true,
login: "wronguser",
password: "wrongpass",
pageNo: 1,
},
{
name: "server error",
responseBody: `{"error":"internal server error"}`,
responseStatus: 500,
expectedError: true,
login: "testuser",
password: "testpass",
pageNo: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify the request
assert.Equal(t, "GET", r.Method)
assert.Contains(t, r.URL.Path, "/v1/domains")
assert.Equal(t, "application/json", r.Header.Get("Accept"))
// Check basic auth
username, password, ok := r.BasicAuth()
assert.True(t, ok, "Basic auth should be present")
assert.Equal(t, tt.login, username)
assert.Equal(t, tt.password, password)
// Check query parameters
assert.Equal(t, "100", r.URL.Query().Get("limit"))
assert.Equal(t, "1", r.URL.Query().Get("page"))
w.WriteHeader(tt.responseStatus)
w.Write([]byte(tt.responseBody))
}))
defer server.Close()
// Create a custom client that uses our test server
client := http.Client{}
// We need to modify the fetchNicmanagerAPI function to accept a custom URL for testing
// For now, let's test the logic by creating a custom version
result, err := fetchNicmanagerAPIWithURL(client, tt.login, tt.password, tt.pageNo, server.URL+"/v1/domains")
if tt.expectedError {
assert.Error(t, err, "Expected an error for status %d", tt.responseStatus)
} else {
require.NoError(t, err, "Unexpected error for successful request")
assert.Equal(t, tt.responseBody, string(result), "Response body should match")
}
})
}
}
// Helper function for testing with custom URL
func fetchNicmanagerAPIWithURL(client http.Client, login string, password string, pageNo int, baseURL string) ([]byte, error) {
req, rErr := http.NewRequest("GET", baseURL+"?limit=100&page="+fmt.Sprintf("%d", pageNo), nil)
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(login, password)
if rErr != nil {
return nil, rErr
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("status code error: %d %s", res.StatusCode, res.Status))
}
return io.ReadAll(res.Body)
}