github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/dns/record_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_CreateRecord(t *testing.T) {
    15  	tests := map[string]struct {
    16  		body           RecordBody
    17  		responseStatus int
    18  		responseBody   string
    19  		expectedPath   string
    20  		withError      error
    21  	}{
    22  		"200 OK": {
    23  			responseStatus: http.StatusCreated,
    24  			body: RecordBody{
    25  				Name:       "www.example.com",
    26  				RecordType: "A",
    27  				TTL:        300,
    28  				Target:     []string{"10.0.0.2", "10.0.0.3"},
    29  			},
    30  			expectedPath: "/config-dns/v2/zones/example.com/names/www.example.com/types/A",
    31  			responseBody: `
    32  			{
    33  				"name": "www.example.com",
    34  				"type": "A",
    35  				"ttl": 300,
    36  				"rdata": [
    37  					"10.0.0.2",
    38  					"10.0.0.3"
    39  				]
    40  			}`,
    41  		},
    42  		"500 internal server error": {
    43  			body: RecordBody{
    44  				Name:       "www.example.com",
    45  				RecordType: "A",
    46  				TTL:        300,
    47  				Target:     []string{"10.0.0.2", "10.0.0.3"},
    48  			},
    49  			responseStatus: http.StatusInternalServerError,
    50  			responseBody: `
    51  {
    52  	"type": "internal_error",
    53      "title": "Internal Server Error",
    54      "detail": "Error fetching authorities",
    55      "status": 500
    56  }`,
    57  			expectedPath: "/config-dns/v2/zones/example.com/names/www.example.com/types/A",
    58  			withError: &Error{
    59  				Type:       "internal_error",
    60  				Title:      "Internal Server Error",
    61  				Detail:     "Error fetching authorities",
    62  				StatusCode: http.StatusInternalServerError,
    63  			},
    64  		},
    65  	}
    66  
    67  	for name, test := range tests {
    68  		t.Run(name, func(t *testing.T) {
    69  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    70  				assert.Equal(t, test.expectedPath, r.URL.String())
    71  				assert.Equal(t, http.MethodPost, r.Method)
    72  				w.WriteHeader(test.responseStatus)
    73  				_, err := w.Write([]byte(test.responseBody))
    74  				assert.NoError(t, err)
    75  			}))
    76  			client := mockAPIClient(t, mockServer)
    77  			err := client.CreateRecord(context.Background(), &test.body, "example.com")
    78  			if test.withError != nil {
    79  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
    80  				return
    81  			}
    82  			require.NoError(t, err)
    83  
    84  		})
    85  	}
    86  }
    87  
    88  func TestDNS_UpdateRecord(t *testing.T) {
    89  	tests := map[string]struct {
    90  		body           RecordBody
    91  		responseStatus int
    92  		responseBody   string
    93  		expectedPath   string
    94  		withError      error
    95  	}{
    96  		"204 No Content": {
    97  			responseStatus: http.StatusOK,
    98  			body: RecordBody{
    99  				Name:       "www.example.com",
   100  				RecordType: "A",
   101  				TTL:        300,
   102  				Target:     []string{"10.0.0.2", "10.0.0.3"},
   103  			},
   104  			expectedPath: "/config-dns/v2/zones/example.com/names/www.example.com/types/A",
   105  			responseBody: `
   106  			{
   107  				"name": "www.example.com",
   108  				"type": "A",
   109  				"ttl": 300,
   110  				"rdata": [
   111  					"10.0.0.2",
   112  					"10.0.0.3"
   113  				]
   114  			}`,
   115  		},
   116  		"500 internal server error": {
   117  			body: RecordBody{
   118  				Name:       "www.example.com",
   119  				RecordType: "A",
   120  				TTL:        300,
   121  				Target:     []string{"10.0.0.2", "10.0.0.3"},
   122  			},
   123  			responseStatus: http.StatusInternalServerError,
   124  			responseBody: `
   125  {
   126  	"type": "internal_error",
   127      "title": "Internal Server Error",
   128      "detail": "Error fetching authorities",
   129      "status": 500
   130  }`,
   131  			expectedPath: "/config-dns/v2/zones/example.com/names/www.example.com/types/A",
   132  			withError: &Error{
   133  				Type:       "internal_error",
   134  				Title:      "Internal Server Error",
   135  				Detail:     "Error fetching authorities",
   136  				StatusCode: http.StatusInternalServerError,
   137  			},
   138  		},
   139  	}
   140  
   141  	for name, test := range tests {
   142  		t.Run(name, func(t *testing.T) {
   143  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   144  				assert.Equal(t, test.expectedPath, r.URL.String())
   145  				assert.Equal(t, http.MethodPut, r.Method)
   146  				w.WriteHeader(test.responseStatus)
   147  				_, err := w.Write([]byte(test.responseBody))
   148  				assert.NoError(t, err)
   149  			}))
   150  			client := mockAPIClient(t, mockServer)
   151  			err := client.UpdateRecord(context.Background(), &test.body, "example.com")
   152  			if test.withError != nil {
   153  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   154  				return
   155  			}
   156  			require.NoError(t, err)
   157  
   158  		})
   159  	}
   160  }
   161  
   162  func TestDNS_DeleteRecord(t *testing.T) {
   163  	tests := map[string]struct {
   164  		body           RecordBody
   165  		responseStatus int
   166  		responseBody   string
   167  		expectedPath   string
   168  		withError      error
   169  	}{
   170  		"204 No Content": {
   171  			responseStatus: http.StatusNoContent,
   172  			body: RecordBody{
   173  				Name:       "www.example.com",
   174  				RecordType: "A",
   175  				TTL:        300,
   176  				Target:     []string{"10.0.0.2", "10.0.0.3"},
   177  			},
   178  			expectedPath: "/config-dns/v2/zones/example.com/names/www.example.com/types/A",
   179  			responseBody: ``,
   180  		},
   181  		"500 internal server error": {
   182  			body: RecordBody{
   183  				Name:       "www.example.com",
   184  				RecordType: "A",
   185  				TTL:        300,
   186  				Target:     []string{"10.0.0.2", "10.0.0.3"},
   187  			},
   188  			responseStatus: http.StatusInternalServerError,
   189  			responseBody: `
   190  {
   191  	"type": "internal_error",
   192      "title": "Internal Server Error",
   193      "detail": "Error fetching authorities",
   194      "status": 500
   195  }`,
   196  			expectedPath: "/config-dns/v2/zones/example.com/names/www.example.com/types/A",
   197  			withError: &Error{
   198  				Type:       "internal_error",
   199  				Title:      "Internal Server Error",
   200  				Detail:     "Error fetching authorities",
   201  				StatusCode: http.StatusInternalServerError,
   202  			},
   203  		},
   204  	}
   205  
   206  	for name, test := range tests {
   207  		t.Run(name, func(t *testing.T) {
   208  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   209  				assert.Equal(t, test.expectedPath, r.URL.String())
   210  				assert.Equal(t, http.MethodDelete, r.Method)
   211  				w.WriteHeader(test.responseStatus)
   212  				_, err := w.Write([]byte(test.responseBody))
   213  				assert.NoError(t, err)
   214  			}))
   215  			client := mockAPIClient(t, mockServer)
   216  			err := client.DeleteRecord(context.Background(), &test.body, "example.com")
   217  			if test.withError != nil {
   218  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   219  				return
   220  			}
   221  			require.NoError(t, err)
   222  
   223  		})
   224  	}
   225  }