github.com/gophercloud/gophercloud@v1.11.0/openstack/dns/v2/transfer/request/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	s "strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/gophercloud/gophercloud"
    11  	transferRequests "github.com/gophercloud/gophercloud/openstack/dns/v2/transfer/request"
    12  	th "github.com/gophercloud/gophercloud/testhelper"
    13  	"github.com/gophercloud/gophercloud/testhelper/client"
    14  )
    15  
    16  // ListOutput is a sample response to a List call.
    17  const ListOutput = `
    18  {
    19      "transfer_requests": [
    20          {
    21              "id": "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3",
    22              "zone_id": "a6a8515c-5d80-48c0-955b-fde631b59791",
    23              "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
    24              "target_project_id": "05d98711-b3a1-4264-a395-f46383671ee6",
    25              "description": "This is a first example zone transfer request.",
    26              "key": "KJSDH23Z",
    27              "status": "ACTIVE",
    28              "zone_name": "example1.org.",
    29              "created_at": "2020-10-12T08:38:58.000000",
    30              "links": {
    31                  "self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3"
    32              }
    33          },
    34          {
    35              "id": "34c4561c-9205-4386-9df5-167436f5a222",
    36              "zone_id": "572ba08c-d929-4c70-8e42-03824bb24ca2",
    37              "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
    38              "target_project_id": "05d98711-b3a1-4264-a395-f46383671ee6",
    39              "description": "This is second example zone transfer request.",
    40              "key": "KSDFJ22H",
    41              "status": "ACTIVE",
    42              "zone_name": "example2.org.",
    43              "created_at": "2020-10-12T09:38:58.000000",
    44              "updated_at": "2020-10-12T10:38:58.000000",
    45              "links": {
    46                  "self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/34c4561c-9205-4386-9df5-167436f5a222"
    47              }
    48          }
    49      ],
    50      "links": {
    51          "self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests"
    52      }
    53  }
    54  `
    55  
    56  // GetOutput is a sample response to a Get call.
    57  const GetOutput = `
    58  {
    59      "id": "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3",
    60      "zone_id": "a6a8515c-5d80-48c0-955b-fde631b59791",
    61      "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
    62      "target_project_id": "05d98711-b3a1-4264-a395-f46383671ee6",
    63      "description": "This is a first example zone transfer request.",
    64      "key": "KJSDH23Z",
    65      "status": "ACTIVE",
    66      "zone_name": "example1.org.",
    67      "created_at": "2020-10-12T08:38:58.000000",
    68      "links": {
    69          "self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3"
    70      }
    71  }
    72  `
    73  
    74  // FirstTransferRequest is the first result in ListOutput
    75  var FirstTransferRequestCreatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2020-10-12T08:38:58.000000")
    76  var FirstTransferRequest = transferRequests.TransferRequest{
    77  	ID:              "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3",
    78  	ZoneID:          "a6a8515c-5d80-48c0-955b-fde631b59791",
    79  	ProjectID:       "4335d1f0-f793-11e2-b778-0800200c9a66",
    80  	TargetProjectID: "05d98711-b3a1-4264-a395-f46383671ee6",
    81  	ZoneName:        "example1.org.",
    82  	Key:             "KJSDH23Z",
    83  	Description:     "This is a first example zone transfer request.",
    84  	Status:          "ACTIVE",
    85  	CreatedAt:       FirstTransferRequestCreatedAt,
    86  	Links: map[string]interface{}{
    87  		"self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3",
    88  	},
    89  }
    90  
    91  // SecondTransferRequest is the second result in ListOutput
    92  var SecondTransferRequestCreatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2020-10-12T09:38:58.000000")
    93  var SecondTransferRequestUpdatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2020-10-12T10:38:58.000000")
    94  var SecondTransferRequest = transferRequests.TransferRequest{
    95  	ID:              "34c4561c-9205-4386-9df5-167436f5a222",
    96  	ZoneID:          "572ba08c-d929-4c70-8e42-03824bb24ca2",
    97  	ProjectID:       "4335d1f0-f793-11e2-b778-0800200c9a66",
    98  	TargetProjectID: "05d98711-b3a1-4264-a395-f46383671ee6",
    99  	ZoneName:        "example2.org.",
   100  	Key:             "KSDFJ22H",
   101  	Description:     "This is second example zone transfer request.",
   102  	Status:          "ACTIVE",
   103  	CreatedAt:       SecondTransferRequestCreatedAt,
   104  	UpdatedAt:       SecondTransferRequestUpdatedAt,
   105  	Links: map[string]interface{}{
   106  		"self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/34c4561c-9205-4386-9df5-167436f5a222",
   107  	},
   108  }
   109  
   110  // ExpectedTransferRequestsSlice is the slice of results that should be parsed
   111  // from ListOutput, in the expected order.
   112  var ExpectedTransferRequestsSlice = []transferRequests.TransferRequest{FirstTransferRequest, SecondTransferRequest}
   113  
   114  // HandleListSuccessfully configures the test server to respond to a List request.
   115  func HandleListSuccessfully(t *testing.T) {
   116  	baseURL := "/zones/tasks/transfer_requests"
   117  	th.Mux.HandleFunc(baseURL,
   118  		func(w http.ResponseWriter, r *http.Request) {
   119  			th.TestMethod(t, r, "GET")
   120  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   121  			w.Header().Add("Content-Type", "application/json")
   122  			fmt.Fprintf(w, ListOutput)
   123  		})
   124  }
   125  
   126  // HandleGetSuccessfully configures the test server to respond to a List request.
   127  func HandleGetSuccessfully(t *testing.T) {
   128  	baseURL := "/zones/tasks/transfer_requests"
   129  	th.Mux.HandleFunc(s.Join([]string{baseURL, FirstTransferRequest.ID}, "/"),
   130  		func(w http.ResponseWriter, r *http.Request) {
   131  			th.TestMethod(t, r, "GET")
   132  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   133  			w.Header().Add("Content-Type", "application/json")
   134  			fmt.Fprintf(w, GetOutput)
   135  		})
   136  }
   137  
   138  // CreateTransferRequest is a sample request to create a zone.
   139  const CreateTransferRequest = `
   140  {
   141      "target_project_id": "05d98711-b3a1-4264-a395-f46383671ee6",
   142      "description": "This is a first example zone transfer request."
   143  }
   144  `
   145  
   146  // CreateZoneResponse is a sample response to a create request.
   147  const CreateTransferRequestResponse = `
   148  {
   149      "id": "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3",
   150      "zone_id": "a6a8515c-5d80-48c0-955b-fde631b59791",
   151      "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
   152      "target_project_id": "05d98711-b3a1-4264-a395-f46383671ee6",
   153      "description": "This is a first example zone transfer request.",
   154      "key": "KJSDH23Z",
   155      "status": "ACTIVE",
   156      "zone_name": "example1.org.",
   157      "created_at": "2020-10-12T08:38:58.000000",
   158      "links": {
   159          "self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3"
   160      }
   161  }
   162  `
   163  
   164  // CreatedTransferRequest is the expected created zone transfer request.
   165  var CreatedTransferRequest = FirstTransferRequest
   166  
   167  // HandleTransferRequestCreationSuccessfully configures the test server to respond to a Create request.
   168  func HandleCreateSuccessfully(t *testing.T) {
   169  	createURL := "/zones/a6a8515c-5d80-48c0-955b-fde631b59791/tasks/transfer_requests"
   170  	th.Mux.HandleFunc(createURL,
   171  		func(w http.ResponseWriter, r *http.Request) {
   172  			th.TestMethod(t, r, "POST")
   173  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   174  			th.TestJSONRequest(t, r, CreateTransferRequest)
   175  
   176  			w.WriteHeader(http.StatusCreated)
   177  			w.Header().Add("Content-Type", "application/json")
   178  			fmt.Fprintf(w, CreateTransferRequestResponse)
   179  		})
   180  }
   181  
   182  // UpdateTransferRequest is a sample request to update a zone transfer request.
   183  const UpdateTransferRequest = `
   184  {
   185      "description": "Updated Description"
   186  }
   187  `
   188  
   189  // UpdatedTransferRequestResponse is a sample response to update a zone transfer request.
   190  const UpdatedTransferRequestResponse = `
   191  {
   192      "id": "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3",
   193      "zone_id": "a6a8515c-5d80-48c0-955b-fde631b59791",
   194      "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
   195      "target_project_id": "05d98711-b3a1-4264-a395-f46383671ee6",
   196      "description": "Updated Description",
   197      "key": "KJSDH23Z",
   198      "status": "ACTIVE",
   199      "zone_name": "example1.org.",
   200      "created_at": "2020-10-12T08:38:58.000000",
   201      "links": {
   202          "self": "https://127.0.0.1:9001/v2/zones/tasks/transfer_requests/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3"
   203      }
   204  }
   205  `
   206  
   207  // HandleTransferRequestUpdateSuccessfully configures the test server to respond to an Update request.
   208  func HandleUpdateSuccessfully(t *testing.T) {
   209  	baseURL := "/zones/tasks/transfer_requests"
   210  	th.Mux.HandleFunc(s.Join([]string{baseURL, FirstTransferRequest.ID}, "/"),
   211  		func(w http.ResponseWriter, r *http.Request) {
   212  			th.TestMethod(t, r, "PATCH")
   213  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   214  			th.TestJSONRequest(t, r, UpdateTransferRequest)
   215  
   216  			w.WriteHeader(http.StatusOK)
   217  			w.Header().Add("Content-Type", "application/json")
   218  			fmt.Fprintf(w, UpdatedTransferRequestResponse)
   219  		})
   220  }
   221  
   222  // HandleTransferRequestDeleteSuccessfully configures the test server to respond to an Delete request.
   223  func HandleDeleteSuccessfully(t *testing.T) {
   224  	baseURL := "/zones/tasks/transfer_requests"
   225  	th.Mux.HandleFunc(s.Join([]string{baseURL, FirstTransferRequest.ID}, "/"),
   226  		func(w http.ResponseWriter, r *http.Request) {
   227  			th.TestMethod(t, r, "DELETE")
   228  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   229  
   230  			w.WriteHeader(http.StatusNoContent)
   231  		})
   232  }