github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/configdns/tsig_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 Test_NewTsigQueryString(t *testing.T) {
    16  	client := Client(session.Must(session.New()))
    17  
    18  	str := client.NewTsigQueryString(context.Background())
    19  
    20  	assert.NotNil(t, str)
    21  }
    22  
    23  func TestDns_ListTsigKeys(t *testing.T) {
    24  	tests := map[string]struct {
    25  		query            TSIGQueryString
    26  		responseStatus   int
    27  		responseBody     string
    28  		expectedPath     string
    29  		expectedResponse *TSIGReportResponse
    30  		withError        error
    31  	}{
    32  		"200 OK": {
    33  			query: TSIGQueryString{
    34  				ContractIds: []string{"1-1ABCD"},
    35  			},
    36  			responseStatus: http.StatusOK,
    37  			responseBody: `
    38  			{
    39  				"metadata": {
    40  					"totalElements": 1
    41  				},
    42  				"keys": [
    43  					{
    44  						"name": "a.test.key.",
    45  						"algorithm": "hmac-sha256",
    46  						"secret": "DjY16JfIi3JnSDosQWE7Xkx60MbCLo1K7hUCqng8ccg=",
    47  						"zonesCount": 3
    48  					}
    49  				]
    50  			}`,
    51  			expectedPath: "/config-dns/v2/keys?contractIds=1-1ABCD",
    52  			expectedResponse: &TSIGReportResponse{
    53  				Metadata: &TSIGReportMeta{
    54  					TotalElements: 1,
    55  				},
    56  				Keys: []*TSIGKeyResponse{
    57  					{
    58  						TSIGKey: TSIGKey{
    59  							Name:      "a.test.key.",
    60  							Algorithm: "hmac-sha256",
    61  							Secret:    "DjY16JfIi3JnSDosQWE7Xkx60MbCLo1K7hUCqng8ccg=",
    62  						},
    63  						ZoneCount: 3,
    64  					},
    65  				},
    66  			},
    67  		},
    68  		"500 internal server error": {
    69  			query: TSIGQueryString{
    70  				ContractIds: []string{"1-1ABCD"},
    71  			},
    72  			responseStatus: http.StatusInternalServerError,
    73  			responseBody: `
    74  {
    75  	"type": "internal_error",
    76      "title": "Internal Server Error",
    77      "detail": "Error fetching authorities",
    78      "status": 500
    79  }`,
    80  			expectedPath: "/config-dns/v2/keys?contractIds=1-1ABCD",
    81  			withError: &Error{
    82  				Type:       "internal_error",
    83  				Title:      "Internal Server Error",
    84  				Detail:     "Error fetching authorities",
    85  				StatusCode: http.StatusInternalServerError,
    86  			},
    87  		},
    88  	}
    89  
    90  	for name, test := range tests {
    91  		t.Run(name, func(t *testing.T) {
    92  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    93  				assert.Equal(t, test.expectedPath, r.URL.String())
    94  				assert.Equal(t, http.MethodGet, r.Method)
    95  				w.WriteHeader(test.responseStatus)
    96  				_, err := w.Write([]byte(test.responseBody))
    97  				assert.NoError(t, err)
    98  			}))
    99  			client := mockAPIClient(t, mockServer)
   100  			result, err := client.ListTsigKeys(context.Background(), &test.query)
   101  			if test.withError != nil {
   102  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   103  				return
   104  			}
   105  			require.NoError(t, err)
   106  			assert.Equal(t, test.expectedResponse, result)
   107  		})
   108  	}
   109  }
   110  
   111  func TestDns_GetTsigKeyZones(t *testing.T) {
   112  	tests := map[string]struct {
   113  		key              TSIGKey
   114  		responseStatus   int
   115  		responseBody     string
   116  		expectedPath     string
   117  		expectedResponse *ZoneNameListResponse
   118  		withError        error
   119  	}{
   120  		"200 OK": {
   121  			key: TSIGKey{
   122  				Name:      "example.com.akamai.com.",
   123  				Algorithm: "hmac-sha512",
   124  				Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   125  			},
   126  			responseStatus: http.StatusOK,
   127  			responseBody: `
   128  			{
   129  				"zones": [
   130  					"river.com",
   131  					"stream.com"
   132  				]
   133  			}`,
   134  			expectedPath: "/config-dns/v2/keys/used-by",
   135  			expectedResponse: &ZoneNameListResponse{
   136  				Zones: []string{"river.com", "stream.com"},
   137  			},
   138  		},
   139  		"500 internal server error": {
   140  			key: TSIGKey{
   141  				Name:      "example.com.akamai.com.",
   142  				Algorithm: "hmac-sha512",
   143  				Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   144  			},
   145  			responseStatus: http.StatusInternalServerError,
   146  			responseBody: `
   147  {
   148  	"type": "internal_error",
   149      "title": "Internal Server Error",
   150      "detail": "Error fetching authorities",
   151      "status": 500
   152  }`,
   153  			expectedPath: "/config-dns/v2/keys/used-by",
   154  			withError: &Error{
   155  				Type:       "internal_error",
   156  				Title:      "Internal Server Error",
   157  				Detail:     "Error fetching authorities",
   158  				StatusCode: http.StatusInternalServerError,
   159  			},
   160  		},
   161  	}
   162  
   163  	for name, test := range tests {
   164  		t.Run(name, func(t *testing.T) {
   165  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   166  				assert.Equal(t, test.expectedPath, r.URL.String())
   167  				assert.Equal(t, http.MethodPost, r.Method)
   168  				w.WriteHeader(test.responseStatus)
   169  				_, err := w.Write([]byte(test.responseBody))
   170  				assert.NoError(t, err)
   171  			}))
   172  			client := mockAPIClient(t, mockServer)
   173  			result, err := client.GetTsigKeyZones(context.Background(), &test.key)
   174  			if test.withError != nil {
   175  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   176  				return
   177  			}
   178  			require.NoError(t, err)
   179  			assert.Equal(t, test.expectedResponse, result)
   180  		})
   181  	}
   182  }
   183  
   184  func TestDns_GetTsigKeyAliases(t *testing.T) {
   185  	tests := map[string]struct {
   186  		zone             string
   187  		responseStatus   int
   188  		responseBody     string
   189  		expectedPath     string
   190  		expectedResponse *ZoneNameListResponse
   191  		withError        error
   192  	}{
   193  		"200 OK": {
   194  			zone:           "example.com",
   195  			responseStatus: http.StatusOK,
   196  			responseBody: `
   197  			{
   198  				"aliases": [
   199  					"exmaple.com",
   200  					"river.com",
   201  					"brook.com",
   202  					"ocean.com"
   203  				]
   204  			}`,
   205  			expectedPath: "/config-dns/v2/zones/example.com/key/used-by",
   206  			expectedResponse: &ZoneNameListResponse{
   207  				Aliases: []string{"exmaple.com", "river.com", "brook.com", "ocean.com"},
   208  			},
   209  		},
   210  		"500 internal server error": {
   211  			zone:           "example.com",
   212  			responseStatus: http.StatusInternalServerError,
   213  			responseBody: `
   214  {
   215  	"type": "internal_error",
   216      "title": "Internal Server Error",
   217      "detail": "Error fetching authorities",
   218      "status": 500
   219  }`,
   220  			expectedPath: "/config-dns/v2/zones/example.com/key/used-by",
   221  			withError: &Error{
   222  				Type:       "internal_error",
   223  				Title:      "Internal Server Error",
   224  				Detail:     "Error fetching authorities",
   225  				StatusCode: http.StatusInternalServerError,
   226  			},
   227  		},
   228  	}
   229  
   230  	for name, test := range tests {
   231  		t.Run(name, func(t *testing.T) {
   232  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   233  				assert.Equal(t, test.expectedPath, r.URL.String())
   234  				assert.Equal(t, http.MethodGet, r.Method)
   235  				w.WriteHeader(test.responseStatus)
   236  				_, err := w.Write([]byte(test.responseBody))
   237  				assert.NoError(t, err)
   238  			}))
   239  			client := mockAPIClient(t, mockServer)
   240  			result, err := client.GetTsigKeyAliases(context.Background(), test.zone)
   241  			if test.withError != nil {
   242  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   243  				return
   244  			}
   245  			require.NoError(t, err)
   246  			assert.Equal(t, test.expectedResponse, result)
   247  		})
   248  	}
   249  }
   250  
   251  func TestDns_TsigKeyBulkUpdate(t *testing.T) {
   252  	tests := map[string]struct {
   253  		bulk             TSIGKeyBulkPost
   254  		responseStatus   int
   255  		responseBody     string
   256  		expectedPath     string
   257  		expectedResponse *ZoneNameListResponse
   258  		withError        error
   259  	}{
   260  		"200 OK": {
   261  			bulk: TSIGKeyBulkPost{
   262  				Key: &TSIGKey{
   263  					Name:      "brook.com.akamai.com.",
   264  					Algorithm: "hmac-sha512",
   265  					Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   266  				},
   267  				Zones: []string{"brook.com", "river.com"},
   268  			},
   269  			responseStatus: http.StatusNoContent,
   270  			expectedPath:   "/config-dns/v2/keys/bulk-update",
   271  			expectedResponse: &ZoneNameListResponse{
   272  				Aliases: []string{"exmaple.com", "river.com", "brook.com", "ocean.com"},
   273  			},
   274  		},
   275  		"500 internal server error": {
   276  			bulk: TSIGKeyBulkPost{
   277  				Key: &TSIGKey{
   278  					Name:      "brook.com.akamai.com.",
   279  					Algorithm: "hmac-sha512",
   280  					Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   281  				},
   282  				Zones: []string{"brook.com", "river.com"},
   283  			},
   284  			responseStatus: http.StatusInternalServerError,
   285  			responseBody: `
   286  {
   287  	"type": "internal_error",
   288      "title": "Internal Server Error",
   289      "detail": "Error fetching authorities",
   290      "status": 500
   291  }`,
   292  			expectedPath: "/config-dns/v2/keys/bulk-update",
   293  			withError: &Error{
   294  				Type:       "internal_error",
   295  				Title:      "Internal Server Error",
   296  				Detail:     "Error fetching authorities",
   297  				StatusCode: http.StatusInternalServerError,
   298  			},
   299  		},
   300  	}
   301  
   302  	for name, test := range tests {
   303  		t.Run(name, func(t *testing.T) {
   304  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   305  				assert.Equal(t, test.expectedPath, r.URL.String())
   306  				assert.Equal(t, http.MethodPost, r.Method)
   307  				w.WriteHeader(test.responseStatus)
   308  				if len(test.responseBody) > 0 {
   309  					_, err := w.Write([]byte(test.responseBody))
   310  					assert.NoError(t, err)
   311  				}
   312  			}))
   313  			client := mockAPIClient(t, mockServer)
   314  			err := client.TsigKeyBulkUpdate(context.Background(), &test.bulk)
   315  			if test.withError != nil {
   316  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   317  				return
   318  			}
   319  			require.NoError(t, err)
   320  		})
   321  	}
   322  }
   323  
   324  func TestDns_GetTsigKey(t *testing.T) {
   325  	tests := map[string]struct {
   326  		zone             string
   327  		responseStatus   int
   328  		responseBody     string
   329  		expectedPath     string
   330  		expectedResponse *TSIGKeyResponse
   331  		withError        error
   332  	}{
   333  		"200 OK": {
   334  			zone:           "example.com",
   335  			responseStatus: http.StatusOK,
   336  			responseBody: `
   337  			{
   338  				"name": "example.com.akamai.com.",
   339  				"algorithm": "hmac-sha512",
   340  				"secret": "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   341  				"zonesCount": 7
   342  			}`,
   343  			expectedPath: "/config-dns/v2/zones/example.com/key",
   344  			expectedResponse: &TSIGKeyResponse{
   345  				TSIGKey: TSIGKey{
   346  					Name:      "example.com.akamai.com.",
   347  					Algorithm: "hmac-sha512",
   348  					Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   349  				},
   350  				ZoneCount: 7,
   351  			},
   352  		},
   353  		"500 internal server error": {
   354  			zone:           "example.com",
   355  			responseStatus: http.StatusInternalServerError,
   356  			responseBody: `
   357  {
   358  	"type": "internal_error",
   359      "title": "Internal Server Error",
   360      "detail": "Error fetching authorities",
   361      "status": 500
   362  }`,
   363  			expectedPath: "/config-dns/v2/zones/example.com/key",
   364  			withError: &Error{
   365  				Type:       "internal_error",
   366  				Title:      "Internal Server Error",
   367  				Detail:     "Error fetching authorities",
   368  				StatusCode: http.StatusInternalServerError,
   369  			},
   370  		},
   371  	}
   372  
   373  	for name, test := range tests {
   374  		t.Run(name, func(t *testing.T) {
   375  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   376  				assert.Equal(t, test.expectedPath, r.URL.String())
   377  				assert.Equal(t, http.MethodGet, r.Method)
   378  				w.WriteHeader(test.responseStatus)
   379  				_, err := w.Write([]byte(test.responseBody))
   380  				assert.NoError(t, err)
   381  			}))
   382  			client := mockAPIClient(t, mockServer)
   383  			result, err := client.GetTsigKey(context.Background(), test.zone)
   384  			if test.withError != nil {
   385  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   386  				return
   387  			}
   388  			require.NoError(t, err)
   389  			assert.Equal(t, test.expectedResponse, result)
   390  		})
   391  	}
   392  }
   393  
   394  func TestDns_DeleteTsigKey(t *testing.T) {
   395  	tests := map[string]struct {
   396  		zone             string
   397  		responseStatus   int
   398  		responseBody     string
   399  		expectedPath     string
   400  		expectedResponse *TSIGKeyResponse
   401  		withError        error
   402  	}{
   403  		"204 No Content": {
   404  			zone:           "example.com",
   405  			responseStatus: http.StatusNoContent,
   406  			expectedPath:   "/config-dns/v2/zones/example.com/key",
   407  		},
   408  		"500 internal server error": {
   409  			zone:           "example.com",
   410  			responseStatus: http.StatusInternalServerError,
   411  			responseBody: `
   412  {
   413  	"type": "internal_error",
   414      "title": "Internal Server Error",
   415      "detail": "Error Deleting TSig Key",
   416      "status": 500
   417  }`,
   418  			expectedPath: "/config-dns/v2/zones/example.com/key",
   419  			withError: &Error{
   420  				Type:       "internal_error",
   421  				Title:      "Internal Server Error",
   422  				Detail:     "Error Deleting TSig Key",
   423  				StatusCode: http.StatusInternalServerError,
   424  			},
   425  		},
   426  	}
   427  
   428  	for name, test := range tests {
   429  		t.Run(name, func(t *testing.T) {
   430  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   431  				assert.Equal(t, test.expectedPath, r.URL.String())
   432  				assert.Equal(t, http.MethodDelete, r.Method)
   433  				w.WriteHeader(test.responseStatus)
   434  				if len(test.responseBody) > 0 {
   435  					_, err := w.Write([]byte(test.responseBody))
   436  					assert.NoError(t, err)
   437  				}
   438  			}))
   439  			client := mockAPIClient(t, mockServer)
   440  			err := client.DeleteTsigKey(context.Background(), test.zone)
   441  			if test.withError != nil {
   442  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   443  				return
   444  			}
   445  			require.NoError(t, err)
   446  		})
   447  	}
   448  }
   449  
   450  func TestDns_UpdateTsigKey(t *testing.T) {
   451  	tests := map[string]struct {
   452  		key              TSIGKey
   453  		zone             string
   454  		responseStatus   int
   455  		responseBody     string
   456  		expectedPath     string
   457  		expectedResponse *TSIGKeyResponse
   458  		withError        error
   459  	}{
   460  		"200 OK": {
   461  			key: TSIGKey{
   462  				Name:      "example.com.akamai.com.",
   463  				Algorithm: "hmac-sha512",
   464  				Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   465  			},
   466  			zone:           "example.com",
   467  			responseStatus: http.StatusNoContent,
   468  			expectedPath:   "/config-dns/v2/zones/example.com/key",
   469  		},
   470  		"500 internal server error": {
   471  			key: TSIGKey{
   472  				Name:      "example.com.akamai.com.",
   473  				Algorithm: "hmac-sha512",
   474  				Secret:    "Ok1qR5IW1ajVka5cHPEJQIXfLyx5V3PSkFBROAzOn21JumDq6nIpoj6H8rfj5Uo+Ok55ZWQ0Wgrf302fDscHLw==",
   475  			},
   476  			zone:           "example.com",
   477  			responseStatus: http.StatusInternalServerError,
   478  			responseBody: `
   479  {
   480  	"type": "internal_error",
   481      "title": "Internal Server Error",
   482      "detail": "Error fetching authorities",
   483      "status": 500
   484  }`,
   485  			expectedPath: "/config-dns/v2/zones/example.com/key",
   486  			withError: &Error{
   487  				Type:       "internal_error",
   488  				Title:      "Internal Server Error",
   489  				Detail:     "Error fetching authorities",
   490  				StatusCode: http.StatusInternalServerError,
   491  			},
   492  		},
   493  	}
   494  
   495  	for name, test := range tests {
   496  		t.Run(name, func(t *testing.T) {
   497  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   498  				assert.Equal(t, test.expectedPath, r.URL.String())
   499  				assert.Equal(t, http.MethodPut, r.Method)
   500  				w.WriteHeader(test.responseStatus)
   501  				if len(test.responseBody) > 0 {
   502  					_, err := w.Write([]byte(test.responseBody))
   503  					assert.NoError(t, err)
   504  				}
   505  			}))
   506  			client := mockAPIClient(t, mockServer)
   507  			err := client.UpdateTsigKey(context.Background(), &test.key, test.zone)
   508  			if test.withError != nil {
   509  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   510  				return
   511  			}
   512  			require.NoError(t, err)
   513  		})
   514  	}
   515  }