|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func setupTest(t *testing.T, pattern string, handler http.HandlerFunc) *Client { |
| 19 | + t.Helper() |
| 20 | + |
| 21 | + mux := http.NewServeMux() |
| 22 | + server := httptest.NewServer(mux) |
| 23 | + t.Cleanup(server.Close) |
| 24 | + |
| 25 | + mux.HandleFunc(pattern, handler) |
| 26 | + |
| 27 | + client, err := NewClient(server.Client()) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + client.baseURL, _ = url.Parse(server.URL) |
| 31 | + |
| 32 | + return client |
| 33 | +} |
| 34 | + |
| 35 | +func writeFixtures(method string, filename string, status int) http.HandlerFunc { |
| 36 | + return func(rw http.ResponseWriter, req *http.Request) { |
| 37 | + if req.Method != method { |
| 38 | + http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + file, err := os.Open(filepath.Join("fixtures", filename)) |
| 43 | + if err != nil { |
| 44 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + defer func() { _ = file.Close() }() |
| 49 | + |
| 50 | + rw.WriteHeader(status) |
| 51 | + _, err = io.Copy(rw, file) |
| 52 | + if err != nil { |
| 53 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 54 | + return |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestClient_GetServices(t *testing.T) { |
| 60 | + client := setupTest(t, "/services", |
| 61 | + writeFixtures(http.MethodGet, "services_GET.xml", http.StatusOK)) |
| 62 | + |
| 63 | + zones, err := client.GetServices(context.Background()) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + expected := []Service{ |
| 67 | + { |
| 68 | + Admin: "123/NIC-REG", |
| 69 | + DomainsLimit: "12", |
| 70 | + DomainsNum: "5", |
| 71 | + Enable: "true", |
| 72 | + HasPrimary: "false", |
| 73 | + Name: "testservice", |
| 74 | + Payer: "123/NIC-REG", |
| 75 | + Tariff: "Secondary L", |
| 76 | + }, |
| 77 | + { |
| 78 | + Admin: "123/NIC-REG", |
| 79 | + DomainsLimit: "150", |
| 80 | + DomainsNum: "10", |
| 81 | + Enable: "true", |
| 82 | + HasPrimary: "true", |
| 83 | + Name: "myservice", |
| 84 | + Payer: "123/NIC-REG", |
| 85 | + Tariff: "DNS-master XXL", |
| 86 | + RRLimit: "7500", |
| 87 | + RRNum: "1000", |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + assert.Equal(t, expected, zones) |
| 92 | +} |
| 93 | + |
| 94 | +func TestClient_ListZones(t *testing.T) { |
| 95 | + client := setupTest(t, "/zones", |
| 96 | + writeFixtures(http.MethodGet, "zones_all_GET.xml", http.StatusOK)) |
| 97 | + |
| 98 | + zones, err := client.ListZones(context.Background()) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + expected := []Zone{ |
| 102 | + { |
| 103 | + Admin: "123/NIC-REG", |
| 104 | + Enable: "true", |
| 105 | + HasChanges: "false", |
| 106 | + HasPrimary: "true", |
| 107 | + ID: "227645", |
| 108 | + IDNName: "тест.рф", |
| 109 | + Name: "xn—e1aybc.xn--p1ai", |
| 110 | + Payer: "123/NIC-REG", |
| 111 | + Service: "myservice", |
| 112 | + }, |
| 113 | + { |
| 114 | + Admin: "123/NIC-REG", |
| 115 | + Enable: "true", |
| 116 | + HasChanges: "false", |
| 117 | + HasPrimary: "true", |
| 118 | + ID: "227642", |
| 119 | + IDNName: "example.ru", |
| 120 | + Name: "example.ru", |
| 121 | + Payer: "123/NIC-REG", |
| 122 | + Service: "myservice", |
| 123 | + }, |
| 124 | + { |
| 125 | + Admin: "123/NIC-REG", |
| 126 | + Enable: "true", |
| 127 | + HasChanges: "false", |
| 128 | + HasPrimary: "true", |
| 129 | + ID: "227643", |
| 130 | + IDNName: "test.su", |
| 131 | + Name: "test.su", |
| 132 | + Payer: "123/NIC-REG", |
| 133 | + Service: "myservice", |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + assert.Equal(t, expected, zones) |
| 138 | +} |
| 139 | + |
| 140 | +func TestClient_ListZones_error(t *testing.T) { |
| 141 | + client := setupTest(t, "/zones", |
| 142 | + writeFixtures(http.MethodGet, "errors.xml", http.StatusOK)) |
| 143 | + |
| 144 | + _, err := client.ListZones(context.Background()) |
| 145 | + require.ErrorIs(t, err, Error{ |
| 146 | + Text: "Access token expired or not found", |
| 147 | + Code: "4097", |
| 148 | + }) |
| 149 | +} |
| 150 | + |
| 151 | +func TestClient_GetZonesByService(t *testing.T) { |
| 152 | + client := setupTest(t, "/services/test/zones", |
| 153 | + writeFixtures(http.MethodGet, "zones_GET.xml", http.StatusOK)) |
| 154 | + |
| 155 | + zones, err := client.GetZonesByService(context.Background(), "test") |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + expected := []Zone{ |
| 159 | + { |
| 160 | + Admin: "123/NIC-REG", |
| 161 | + Enable: "true", |
| 162 | + HasChanges: "false", |
| 163 | + HasPrimary: "true", |
| 164 | + ID: "227645", |
| 165 | + IDNName: "тест.рф", |
| 166 | + Name: "xn—e1aybc.xn--p1ai", |
| 167 | + Payer: "123/NIC-REG", |
| 168 | + Service: "myservice", |
| 169 | + }, |
| 170 | + { |
| 171 | + Admin: "123/NIC-REG", |
| 172 | + Enable: "true", |
| 173 | + HasChanges: "false", |
| 174 | + HasPrimary: "true", |
| 175 | + ID: "227642", |
| 176 | + IDNName: "example.ru", |
| 177 | + Name: "example.ru", |
| 178 | + Payer: "123/NIC-REG", |
| 179 | + Service: "myservice", |
| 180 | + }, |
| 181 | + { |
| 182 | + Admin: "123/NIC-REG", |
| 183 | + Enable: "true", |
| 184 | + HasChanges: "false", |
| 185 | + HasPrimary: "true", |
| 186 | + ID: "227643", |
| 187 | + IDNName: "test.su", |
| 188 | + Name: "test.su", |
| 189 | + Payer: "123/NIC-REG", |
| 190 | + Service: "myservice", |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | + assert.Equal(t, expected, zones) |
| 195 | +} |
| 196 | + |
| 197 | +func TestClient_GetZonesByService_error(t *testing.T) { |
| 198 | + client := setupTest(t, "/services/test/zones", |
| 199 | + writeFixtures(http.MethodGet, "errors.xml", http.StatusOK)) |
| 200 | + |
| 201 | + _, err := client.GetZonesByService(context.Background(), "test") |
| 202 | + require.ErrorIs(t, err, Error{ |
| 203 | + Text: "Access token expired or not found", |
| 204 | + Code: "4097", |
| 205 | + }) |
| 206 | +} |
| 207 | + |
| 208 | +func TestClient_GetRecords(t *testing.T) { |
| 209 | + client := setupTest(t, "/services/test/zones/example.com./records", |
| 210 | + writeFixtures(http.MethodGet, "records_GET.xml", http.StatusOK)) |
| 211 | + |
| 212 | + records, err := client.GetRecords(context.Background(), "test", "example.com.") |
| 213 | + require.NoError(t, err) |
| 214 | + |
| 215 | + expected := []RR{ |
| 216 | + { |
| 217 | + ID: "210074", |
| 218 | + Name: "@", |
| 219 | + IDNName: "@", |
| 220 | + TTL: "", |
| 221 | + Type: "SOA", |
| 222 | + SOA: &SOA{ |
| 223 | + MName: &MName{ |
| 224 | + Name: "ns3-l2.nic.ru.", |
| 225 | + IDNName: "ns3-l2.nic.ru.", |
| 226 | + }, |
| 227 | + RName: &RName{ |
| 228 | + Name: "dns.nic.ru.", |
| 229 | + IDNName: "dns.nic.ru.", |
| 230 | + }, |
| 231 | + Serial: "2011112002", |
| 232 | + Refresh: "1440", |
| 233 | + Retry: "3600", |
| 234 | + Expire: "2592000", |
| 235 | + Minimum: "600", |
| 236 | + }, |
| 237 | + }, |
| 238 | + { |
| 239 | + ID: "210075", |
| 240 | + Name: "@", |
| 241 | + IDNName: "@", |
| 242 | + Type: "NS", |
| 243 | + NS: &NS{ |
| 244 | + Name: "ns3-l2.nic.ru.", |
| 245 | + IDNName: "ns3- l2.nic.ru.", |
| 246 | + }, |
| 247 | + }, |
| 248 | + { |
| 249 | + ID: "210076", |
| 250 | + Name: "@", |
| 251 | + IDNName: "@", |
| 252 | + Type: "NS", |
| 253 | + NS: &NS{ |
| 254 | + Name: "ns4-l2.nic.ru.", |
| 255 | + IDNName: "ns4-l2.nic.ru.", |
| 256 | + }, |
| 257 | + }, |
| 258 | + { |
| 259 | + ID: "210077", |
| 260 | + Name: "@", |
| 261 | + IDNName: "@", |
| 262 | + Type: "NS", |
| 263 | + NS: &NS{ |
| 264 | + Name: "ns8-l2.nic.ru.", |
| 265 | + IDNName: "ns8- l2.nic.ru.", |
| 266 | + }, |
| 267 | + }, |
| 268 | + } |
| 269 | + |
| 270 | + assert.Equal(t, expected, records) |
| 271 | +} |
| 272 | + |
| 273 | +func TestClient_GetRecords_error(t *testing.T) { |
| 274 | + client := setupTest(t, "/services/test/zones/example.com./records", |
| 275 | + writeFixtures(http.MethodGet, "errors.xml", http.StatusOK)) |
| 276 | + |
| 277 | + _, err := client.GetRecords(context.Background(), "test", "example.com.") |
| 278 | + require.ErrorIs(t, err, Error{ |
| 279 | + Text: "Access token expired or not found", |
| 280 | + Code: "4097", |
| 281 | + }) |
| 282 | +} |
| 283 | + |
| 284 | +func TestClient_AddRecord(t *testing.T) { |
| 285 | + client := setupTest(t, "/services/test/zones/example.com./records", |
| 286 | + writeFixtures(http.MethodPut, "records_PUT.xml", http.StatusOK)) |
| 287 | + |
| 288 | + rrs := []RR{ |
| 289 | + { |
| 290 | + Name: "@", |
| 291 | + Type: "NS", |
| 292 | + NS: &NS{Name: "ns4-l2.nic.ru."}, |
| 293 | + }, |
| 294 | + { |
| 295 | + Name: "@", |
| 296 | + Type: "NS", |
| 297 | + NS: &NS{Name: "ns8-l2.nic.ru."}, |
| 298 | + }, |
| 299 | + } |
| 300 | + |
| 301 | + response, err := client.AddRecords(context.Background(), "test", "example.com.", rrs) |
| 302 | + require.NoError(t, err) |
| 303 | + |
| 304 | + expected := []Zone{ |
| 305 | + { |
| 306 | + Admin: "123/NIC-REG", |
| 307 | + HasChanges: "true", |
| 308 | + ID: "228095", |
| 309 | + IDNName: "test.ru", |
| 310 | + Name: "test.ru", |
| 311 | + Service: "testservice", |
| 312 | + RR: []RR{ |
| 313 | + { |
| 314 | + ID: "210076", |
| 315 | + Name: "@", |
| 316 | + IDNName: "@", |
| 317 | + Type: "NS", |
| 318 | + NS: &NS{ |
| 319 | + Name: "ns4-l2.nic.ru.", |
| 320 | + IDNName: "ns4-l2.nic.ru.", |
| 321 | + }, |
| 322 | + }, |
| 323 | + { |
| 324 | + ID: "210077", |
| 325 | + Name: "@", |
| 326 | + IDNName: "@", |
| 327 | + Type: "NS", |
| 328 | + NS: &NS{ |
| 329 | + Name: "ns8-l2.nic.ru.", |
| 330 | + IDNName: "ns8-l2.nic.ru.", |
| 331 | + }, |
| 332 | + }, |
| 333 | + }, |
| 334 | + }, |
| 335 | + } |
| 336 | + |
| 337 | + assert.Equal(t, expected, response) |
| 338 | +} |
| 339 | + |
| 340 | +func TestClient_AddRecord_error(t *testing.T) { |
| 341 | + client := setupTest(t, "/services/test/zones/example.com./records", |
| 342 | + writeFixtures(http.MethodPut, "errors.xml", http.StatusOK)) |
| 343 | + |
| 344 | + rrs := []RR{ |
| 345 | + { |
| 346 | + Name: "@", |
| 347 | + Type: "NS", |
| 348 | + NS: &NS{Name: "ns4-l2.nic.ru."}, |
| 349 | + }, |
| 350 | + { |
| 351 | + Name: "@", |
| 352 | + Type: "NS", |
| 353 | + NS: &NS{Name: "ns8-l2.nic.ru."}, |
| 354 | + }, |
| 355 | + } |
| 356 | + |
| 357 | + _, err := client.AddRecords(context.Background(), "test", "example.com.", rrs) |
| 358 | + require.ErrorIs(t, err, Error{ |
| 359 | + Text: "Access token expired or not found", |
| 360 | + Code: "4097", |
| 361 | + }) |
| 362 | +} |
| 363 | + |
| 364 | +func TestClient_DeleteRecord(t *testing.T) { |
| 365 | + client := setupTest(t, "/services/test/zones/example.com./records/123", |
| 366 | + writeFixtures(http.MethodDelete, "record_DELETE.xml", http.StatusUnauthorized)) |
| 367 | + |
| 368 | + err := client.DeleteRecord(context.Background(), "test", "example.com.", "123") |
| 369 | + require.NoError(t, err) |
| 370 | +} |
| 371 | + |
| 372 | +func TestClient_DeleteRecord_error(t *testing.T) { |
| 373 | + client := setupTest(t, "/services/test/zones/example.com./records/123", |
| 374 | + writeFixtures(http.MethodDelete, "errors.xml", http.StatusUnauthorized)) |
| 375 | + |
| 376 | + err := client.DeleteRecord(context.Background(), "test", "example.com.", "123") |
| 377 | + require.ErrorIs(t, err, Error{ |
| 378 | + Text: "Access token expired or not found", |
| 379 | + Code: "4097", |
| 380 | + }) |
| 381 | +} |
| 382 | + |
| 383 | +func TestClient_CommitZone(t *testing.T) { |
| 384 | + client := setupTest(t, "/services/test/zones/example.com./commit", writeFixtures(http.MethodPost, "commit_POST.xml", http.StatusOK)) |
| 385 | + |
| 386 | + err := client.CommitZone(context.Background(), "test", "example.com.") |
| 387 | + require.NoError(t, err) |
| 388 | +} |
| 389 | + |
| 390 | +func TestClient_CommitZone_error(t *testing.T) { |
| 391 | + client := setupTest(t, "/services/test/zones/example.com./commit", writeFixtures(http.MethodPost, "errors.xml", http.StatusOK)) |
| 392 | + |
| 393 | + err := client.CommitZone(context.Background(), "test", "example.com.") |
| 394 | + require.ErrorIs(t, err, Error{ |
| 395 | + Text: "Access token expired or not found", |
| 396 | + Code: "4097", |
| 397 | + }) |
| 398 | +} |
0 commit comments