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