github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/dns/authorities_test.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestDNS_GetAuthorities(t *testing.T) {
    15  	tests := map[string]struct {
    16  		contractID       string
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *AuthorityResponse
    21  		withError        error
    22  	}{
    23  		"200 OK": {
    24  			contractID:     "9-9XXXXX",
    25  			responseStatus: http.StatusOK,
    26  			responseBody: `
    27  {
    28  	"contracts": [
    29          {
    30              "contractId": "9-9XXXXX",
    31              "authorities": [
    32                  "a1-118.akam.net.",
    33                  "a2-64.akam.net.",
    34                  "a6-66.akam.net.",
    35                  "a18-67.akam.net.",
    36                  "a7-64.akam.net.",
    37                  "a11-64.akam.net."
    38              ]
    39          }
    40      ]
    41  }`,
    42  			expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX",
    43  			expectedResponse: &AuthorityResponse{
    44  				Contracts: []Contract{
    45  					{
    46  						ContractID: "9-9XXXXX",
    47  						Authorities: []string{
    48  							"a1-118.akam.net.",
    49  							"a2-64.akam.net.",
    50  							"a6-66.akam.net.",
    51  							"a18-67.akam.net.",
    52  							"a7-64.akam.net.",
    53  							"a11-64.akam.net.",
    54  						},
    55  					},
    56  				},
    57  			},
    58  		},
    59  		"Missing arguments": {
    60  			contractID:     "",
    61  			responseStatus: http.StatusOK,
    62  			responseBody:   "",
    63  			expectedPath:   "/config-dns/v2/data/authorities?contractIds=9-9XXXXX",
    64  			withError:      ErrBadRequest,
    65  		},
    66  		"500 internal server error": {
    67  			contractID:     "9-9XXXXX",
    68  			responseStatus: http.StatusInternalServerError,
    69  			responseBody: `
    70  {
    71  	"type": "internal_error",
    72      "title": "Internal Server Error",
    73      "detail": "Error fetching authorities",
    74      "status": 500
    75  }`,
    76  			expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX",
    77  			withError: &Error{
    78  				Type:       "internal_error",
    79  				Title:      "Internal Server Error",
    80  				Detail:     "Error fetching authorities",
    81  				StatusCode: http.StatusInternalServerError,
    82  			},
    83  		},
    84  	}
    85  
    86  	for name, test := range tests {
    87  		t.Run(name, func(t *testing.T) {
    88  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    89  				assert.Equal(t, test.expectedPath, r.URL.String())
    90  				assert.Equal(t, http.MethodGet, r.Method)
    91  				w.WriteHeader(test.responseStatus)
    92  				_, err := w.Write([]byte(test.responseBody))
    93  				assert.NoError(t, err)
    94  			}))
    95  			client := mockAPIClient(t, mockServer)
    96  			result, err := client.GetAuthorities(context.Background(), test.contractID)
    97  			if test.withError != nil {
    98  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
    99  				return
   100  			}
   101  			require.NoError(t, err)
   102  			assert.Equal(t, test.expectedResponse, result)
   103  		})
   104  	}
   105  }
   106  
   107  func TestDNS_GetNameServerRecordList(t *testing.T) {
   108  	tests := map[string]struct {
   109  		contractID       string
   110  		responseStatus   int
   111  		responseBody     string
   112  		expectedPath     string
   113  		expectedResponse []string
   114  		withError        error
   115  	}{
   116  		"test with valid arguments": {
   117  			contractID:     "9-9XXXXX",
   118  			responseStatus: http.StatusOK,
   119  			responseBody: `
   120  {
   121  	"contracts": [
   122          {
   123              "contractId": "9-9XXXXX",
   124              "authorities": [
   125                  "a1-118.akam.net.",
   126                  "a2-64.akam.net.",
   127                  "a6-66.akam.net.",
   128                  "a18-67.akam.net.",
   129                  "a7-64.akam.net.",
   130                  "a11-64.akam.net."
   131              ]
   132          }
   133      ]
   134  }`,
   135  			expectedResponse: []string{"a1-118.akam.net.", "a2-64.akam.net.", "a6-66.akam.net.", "a18-67.akam.net.", "a7-64.akam.net.", "a11-64.akam.net."},
   136  			expectedPath:     "/config-dns/v2/data/authorities?contractIds=9-9XXXXX",
   137  		},
   138  		"test with missing arguments": {
   139  			contractID:   "",
   140  			expectedPath: "/config-dns/v2/data/authorities?contractIds=9-9XXXXX",
   141  			withError:    ErrBadRequest,
   142  		},
   143  	}
   144  
   145  	for name, test := range tests {
   146  		t.Run(name, func(t *testing.T) {
   147  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   148  				assert.Equal(t, test.expectedPath, r.URL.String())
   149  				assert.Equal(t, http.MethodGet, r.Method)
   150  				w.WriteHeader(test.responseStatus)
   151  				_, err := w.Write([]byte(test.responseBody))
   152  				assert.NoError(t, err)
   153  			}))
   154  			client := mockAPIClient(t, mockServer)
   155  			result, err := client.GetNameServerRecordList(context.Background(), test.contractID)
   156  			if test.withError != nil {
   157  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   158  				return
   159  			}
   160  			require.NoError(t, err)
   161  			assert.Equal(t, test.expectedResponse, result)
   162  		})
   163  	}
   164  }