github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v2/tenants/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/v2/tenants"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  // ListOutput provides a single page of Tenant results.
    14  const ListOutput = `
    15  {
    16  	"tenants": [
    17  		{
    18  			"id": "1234",
    19  			"name": "Red Team",
    20  			"description": "The team that is red",
    21  			"enabled": true
    22  		},
    23  		{
    24  			"id": "9876",
    25  			"name": "Blue Team",
    26  			"description": "The team that is blue",
    27  			"enabled": false
    28  		}
    29  	]
    30  }
    31  `
    32  
    33  // RedTeam is a Tenant fixture.
    34  var RedTeam = tenants.Tenant{
    35  	ID:          "1234",
    36  	Name:        "Red Team",
    37  	Description: "The team that is red",
    38  	Enabled:     true,
    39  }
    40  
    41  // BlueTeam is a Tenant fixture.
    42  var BlueTeam = tenants.Tenant{
    43  	ID:          "9876",
    44  	Name:        "Blue Team",
    45  	Description: "The team that is blue",
    46  	Enabled:     false,
    47  }
    48  
    49  // ExpectedTenantSlice is the slice of tenants expected to be returned from ListOutput.
    50  var ExpectedTenantSlice = []tenants.Tenant{RedTeam, BlueTeam}
    51  
    52  // HandleListTenantsSuccessfully creates an HTTP handler at `/tenants` on the test handler mux that
    53  // responds with a list of two tenants.
    54  func HandleListTenantsSuccessfully(t *testing.T) {
    55  	th.Mux.HandleFunc("/tenants", func(w http.ResponseWriter, r *http.Request) {
    56  		th.TestMethod(t, r, "GET")
    57  		th.TestHeader(t, r, "Accept", "application/json")
    58  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    59  
    60  		w.Header().Set("Content-Type", "application/json")
    61  		w.WriteHeader(http.StatusOK)
    62  		fmt.Fprintf(w, ListOutput)
    63  	})
    64  }
    65  
    66  func mockCreateTenantResponse(t *testing.T) {
    67  	th.Mux.HandleFunc("/tenants", func(w http.ResponseWriter, r *http.Request) {
    68  		th.TestMethod(t, r, "POST")
    69  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    70  
    71  		th.TestJSONRequest(t, r, `
    72  {
    73      "tenant": {
    74  		    "name": "new_tenant",
    75  		    "description": "This is new tenant",
    76  		    "enabled": true
    77      }
    78  }
    79  	`)
    80  
    81  		w.Header().Add("Content-Type", "application/json")
    82  		w.WriteHeader(http.StatusOK)
    83  
    84  		fmt.Fprintf(w, `
    85  {
    86      "tenant": {
    87          "name": "new_tenant",
    88          "description": "This is new tenant",
    89          "enabled": true,
    90          "id": "5c62ef576dc7444cbb73b1fe84b97648"
    91      }
    92  }
    93  `)
    94  	})
    95  }
    96  
    97  func mockDeleteTenantResponse(t *testing.T) {
    98  	th.Mux.HandleFunc("/tenants/2466f69cd4714d89a548a68ed97ffcd4", func(w http.ResponseWriter, r *http.Request) {
    99  		th.TestMethod(t, r, "DELETE")
   100  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   101  		w.WriteHeader(http.StatusNoContent)
   102  	})
   103  }
   104  
   105  func mockUpdateTenantResponse(t *testing.T) {
   106  	th.Mux.HandleFunc("/tenants/5c62ef576dc7444cbb73b1fe84b97648", func(w http.ResponseWriter, r *http.Request) {
   107  		th.TestMethod(t, r, "PUT")
   108  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   109  
   110  		th.TestJSONRequest(t, r, `
   111  {
   112      "tenant": {
   113  		    "name": "new_name",
   114  	 	    "description": "This is new name",
   115  		    "enabled": true
   116      }
   117  }
   118  `)
   119  
   120  		w.Header().Add("Content-Type", "application/json")
   121  		w.WriteHeader(http.StatusOK)
   122  
   123  		fmt.Fprintf(w, `
   124  {
   125  		"tenant": {
   126  				"name": "new_name",
   127  				"description": "This is new name",
   128  				"enabled": true,
   129  				"id": "5c62ef576dc7444cbb73b1fe84b97648"
   130  		}
   131  }
   132  `)
   133  	})
   134  }
   135  
   136  func mockGetTenantResponse(t *testing.T) {
   137  	th.Mux.HandleFunc("/tenants/5c62ef576dc7444cbb73b1fe84b97648", func(w http.ResponseWriter, r *http.Request) {
   138  		th.TestMethod(t, r, "GET")
   139  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   140  
   141  		w.Header().Add("Content-Type", "application/json")
   142  		w.WriteHeader(http.StatusOK)
   143  
   144  		fmt.Fprintf(w, `
   145  {
   146  		"tenant": {
   147  				"name": "new_tenant",
   148  				"description": "This is new tenant",
   149  				"enabled": true,
   150  				"id": "5c62ef576dc7444cbb73b1fe84b97648"
   151  		}
   152  }
   153  `)
   154  	})
   155  }