github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/domains/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/identity/v3/domains"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  // ListOutput provides a single page of Domain results.
    14  const ListOutput = `
    15  {
    16      "links": {
    17          "next": null,
    18          "previous": null,
    19          "self": "http://example.com/identity/v3/domains"
    20      },
    21      "domains": [
    22          {
    23              "enabled": true,
    24              "id": "2844b2a08be147a08ef58317d6471f1f",
    25              "links": {
    26                  "self": "http://example.com/identity/v3/domains/2844b2a08be147a08ef58317d6471f1f"
    27              },
    28              "name": "domain one",
    29              "description": "some description"
    30          },
    31          {
    32              "enabled": true,
    33              "id": "9fe1d3",
    34              "links": {
    35                  "self": "https://example.com/identity/v3/domains/9fe1d3"
    36              },
    37              "name": "domain two"
    38          }
    39      ]
    40  }
    41  `
    42  
    43  // GetOutput provides a Get result.
    44  const GetOutput = `
    45  {
    46      "domain": {
    47          "enabled": true,
    48          "id": "9fe1d3",
    49          "links": {
    50              "self": "https://example.com/identity/v3/domains/9fe1d3"
    51          },
    52          "name": "domain two"
    53      }
    54  }
    55  `
    56  
    57  // CreateRequest provides the input to a Create request.
    58  const CreateRequest = `
    59  {
    60      "domain": {
    61          "name": "domain two"
    62      }
    63  }
    64  `
    65  
    66  // UpdateRequest provides the input to as Update request.
    67  const UpdateRequest = `
    68  {
    69      "domain": {
    70          "description": "Staging Domain"
    71      }
    72  }
    73  `
    74  
    75  // UpdateOutput provides an update result.
    76  const UpdateOutput = `
    77  {
    78      "domain": {
    79  		"enabled": true,
    80          "id": "9fe1d3",
    81          "links": {
    82              "self": "https://example.com/identity/v3/domains/9fe1d3"
    83          },
    84          "name": "domain two",
    85          "description": "Staging Domain"
    86      }
    87  }
    88  `
    89  
    90  // FirstDomain is the first domain in the List request.
    91  var FirstDomain = domains.Domain{
    92  	Enabled: true,
    93  	ID:      "2844b2a08be147a08ef58317d6471f1f",
    94  	Links: map[string]interface{}{
    95  		"self": "http://example.com/identity/v3/domains/2844b2a08be147a08ef58317d6471f1f",
    96  	},
    97  	Name:        "domain one",
    98  	Description: "some description",
    99  }
   100  
   101  // SecondDomain is the second domain in the List request.
   102  var SecondDomain = domains.Domain{
   103  	Enabled: true,
   104  	ID:      "9fe1d3",
   105  	Links: map[string]interface{}{
   106  		"self": "https://example.com/identity/v3/domains/9fe1d3",
   107  	},
   108  	Name: "domain two",
   109  }
   110  
   111  // SecondDomainUpdated is how SecondDomain should look after an Update.
   112  var SecondDomainUpdated = domains.Domain{
   113  	Enabled: true,
   114  	ID:      "9fe1d3",
   115  	Links: map[string]interface{}{
   116  		"self": "https://example.com/identity/v3/domains/9fe1d3",
   117  	},
   118  	Name:        "domain two",
   119  	Description: "Staging Domain",
   120  }
   121  
   122  // ExpectedDomainsSlice is the slice of domains expected to be returned from ListOutput.
   123  var ExpectedDomainsSlice = []domains.Domain{FirstDomain, SecondDomain}
   124  
   125  // HandleListDomainsSuccessfully creates an HTTP handler at `/domains` on the
   126  // test handler mux that responds with a list of two domains.
   127  func HandleListDomainsSuccessfully(t *testing.T) {
   128  	th.Mux.HandleFunc("/domains", func(w http.ResponseWriter, r *http.Request) {
   129  		th.TestMethod(t, r, "GET")
   130  		th.TestHeader(t, r, "Accept", "application/json")
   131  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   132  
   133  		w.Header().Set("Content-Type", "application/json")
   134  		w.WriteHeader(http.StatusOK)
   135  		fmt.Fprintf(w, ListOutput)
   136  	})
   137  }
   138  
   139  // HandleGetDomainSuccessfully creates an HTTP handler at `/domains` on the
   140  // test handler mux that responds with a single domain.
   141  func HandleGetDomainSuccessfully(t *testing.T) {
   142  	th.Mux.HandleFunc("/domains/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
   143  		th.TestMethod(t, r, "GET")
   144  		th.TestHeader(t, r, "Accept", "application/json")
   145  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   146  
   147  		w.Header().Set("Content-Type", "application/json")
   148  		w.WriteHeader(http.StatusOK)
   149  		fmt.Fprintf(w, GetOutput)
   150  	})
   151  }
   152  
   153  // HandleCreateDomainSuccessfully creates an HTTP handler at `/domains` on the
   154  // test handler mux that tests domain creation.
   155  func HandleCreateDomainSuccessfully(t *testing.T) {
   156  	th.Mux.HandleFunc("/domains", func(w http.ResponseWriter, r *http.Request) {
   157  		th.TestMethod(t, r, "POST")
   158  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   159  		th.TestJSONRequest(t, r, CreateRequest)
   160  
   161  		w.WriteHeader(http.StatusCreated)
   162  		fmt.Fprintf(w, GetOutput)
   163  	})
   164  }
   165  
   166  // HandleDeleteDomainSuccessfully creates an HTTP handler at `/domains` on the
   167  // test handler mux that tests domain deletion.
   168  func HandleDeleteDomainSuccessfully(t *testing.T) {
   169  	th.Mux.HandleFunc("/domains/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
   170  		th.TestMethod(t, r, "DELETE")
   171  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   172  
   173  		w.WriteHeader(http.StatusNoContent)
   174  	})
   175  }
   176  
   177  // HandleUpdateDomainSuccessfully creates an HTTP handler at `/domains` on the
   178  // test handler mux that tests domain update.
   179  func HandleUpdateDomainSuccessfully(t *testing.T) {
   180  	th.Mux.HandleFunc("/domains/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
   181  		th.TestMethod(t, r, "PATCH")
   182  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   183  		th.TestJSONRequest(t, r, UpdateRequest)
   184  
   185  		w.WriteHeader(http.StatusOK)
   186  		fmt.Fprintf(w, UpdateOutput)
   187  	})
   188  }