github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/limits/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/gophercloud/gophercloud/openstack/identity/v3/limits"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	"github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  const GetEnforcementModelOutput = `
    14  {
    15      "model": {
    16          "description": "Limit enforcement and validation does not take project hierarchy into consideration.",
    17          "name": "flat"
    18      }
    19  }
    20  `
    21  
    22  // ListOutput provides a single page of List results.
    23  const ListOutput = `
    24  {
    25      "links": {
    26          "self": "http://10.3.150.25/identity/v3/limits",
    27          "previous": null,
    28          "next": null
    29      },
    30      "limits": [
    31          {
    32              "resource_name": "volume",
    33              "region_id": null,
    34              "links": {
    35                  "self": "http://10.3.150.25/identity/v3/limits/25a04c7a065c430590881c646cdcdd58"
    36              },
    37              "service_id": "9408080f1970482aa0e38bc2d4ea34b7",
    38              "project_id": "3a705b9f56bb439381b43c4fe59dccce",
    39              "domain_id": null,
    40              "id": "25a04c7a065c430590881c646cdcdd58",
    41              "resource_limit": 11,
    42              "description": "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce"
    43          },
    44          {
    45              "resource_name": "snapshot",
    46              "region_id": "RegionOne",
    47              "links": {
    48                  "self": "http://10.3.150.25/identity/v3/limits/3229b3849f584faea483d6851f7aab05"
    49              },
    50              "service_id": "9408080f1970482aa0e38bc2d4ea34b7",
    51              "project_id": "3a705b9f56bb439381b43c4fe59dccce",
    52              "domain_id": null,
    53              "id": "3229b3849f584faea483d6851f7aab05",
    54              "resource_limit": 5,
    55              "description": null
    56          }
    57      ]
    58  }
    59  `
    60  
    61  const CreateRequest = `
    62  {
    63      "limits":[
    64          {
    65              "service_id": "9408080f1970482aa0e38bc2d4ea34b7",
    66              "project_id": "3a705b9f56bb439381b43c4fe59dccce",
    67              "region_id": "RegionOne",
    68              "resource_name": "snapshot",
    69              "resource_limit": 5
    70          },
    71          {
    72              "service_id": "9408080f1970482aa0e38bc2d4ea34b7",
    73              "domain_id": "edbafc92be354ffa977c58aa79c7bdb2",
    74              "resource_name": "volume",
    75              "resource_limit": 11,
    76              "description": "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce"
    77          }
    78      ]
    79  }
    80  `
    81  
    82  const GetOutput = `
    83  {
    84      "limit": {
    85          "resource_name": "volume",
    86          "region_id": null,
    87          "links": {
    88              "self": "http://10.3.150.25/identity/v3/limits/25a04c7a065c430590881c646cdcdd58"
    89          },
    90          "service_id": "9408080f1970482aa0e38bc2d4ea34b7",
    91          "project_id": "3a705b9f56bb439381b43c4fe59dccce",
    92          "id": "25a04c7a065c430590881c646cdcdd58",
    93          "resource_limit": 11,
    94          "description": "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce"
    95      }
    96  }
    97  `
    98  
    99  const UpdateRequest = `
   100  {
   101      "limit": {
   102          "resource_limit": 5,
   103          "description": "Number of snapshots for project 3a705b9f56bb439381b43c4fe59dccce"
   104      }
   105  }
   106  `
   107  
   108  const UpdateOutput = `
   109  {
   110      "limit": {
   111          "resource_name": "snapshot",
   112          "region_id": "RegionOne",
   113          "links": {
   114              "self": "http://10.3.150.25/identity/v3/limits/3229b3849f584faea483d6851f7aab05"
   115          },
   116          "service_id": "9408080f1970482aa0e38bc2d4ea34b7",
   117          "project_id": "3a705b9f56bb439381b43c4fe59dccce",
   118          "id": "3229b3849f584faea483d6851f7aab05",
   119          "resource_limit": 5,
   120          "description": "Number of snapshots for project 3a705b9f56bb439381b43c4fe59dccce"
   121      }
   122  }
   123  `
   124  
   125  // Model is the enforcement model in the GetEnforcementModel request.
   126  var Model = limits.EnforcementModel{
   127  	Name:        "flat",
   128  	Description: "Limit enforcement and validation does not take project hierarchy into consideration.",
   129  }
   130  
   131  const CreateOutput = ListOutput
   132  
   133  // FirstLimit is the first limit in the List request.
   134  var FirstLimit = limits.Limit{
   135  	ResourceName: "volume",
   136  	Links: map[string]interface{}{
   137  		"self": "http://10.3.150.25/identity/v3/limits/25a04c7a065c430590881c646cdcdd58",
   138  	},
   139  	ServiceID:     "9408080f1970482aa0e38bc2d4ea34b7",
   140  	ProjectID:     "3a705b9f56bb439381b43c4fe59dccce",
   141  	ID:            "25a04c7a065c430590881c646cdcdd58",
   142  	ResourceLimit: 11,
   143  	Description:   "Number of volumes for project 3a705b9f56bb439381b43c4fe59dccce",
   144  }
   145  
   146  // SecondLimit is the second limit in the List request.
   147  var SecondLimit = limits.Limit{
   148  	ResourceName: "snapshot",
   149  	RegionID:     "RegionOne",
   150  	Links: map[string]interface{}{
   151  		"self": "http://10.3.150.25/identity/v3/limits/3229b3849f584faea483d6851f7aab05",
   152  	},
   153  	ServiceID:     "9408080f1970482aa0e38bc2d4ea34b7",
   154  	ProjectID:     "3a705b9f56bb439381b43c4fe59dccce",
   155  	ID:            "3229b3849f584faea483d6851f7aab05",
   156  	ResourceLimit: 5,
   157  }
   158  
   159  // SecondLimitUpdated is the updated limit in the Update request.
   160  var SecondLimitUpdated = limits.Limit{
   161  	ResourceName: "snapshot",
   162  	RegionID:     "RegionOne",
   163  	Links: map[string]interface{}{
   164  		"self": "http://10.3.150.25/identity/v3/limits/3229b3849f584faea483d6851f7aab05",
   165  	},
   166  	ServiceID:     "9408080f1970482aa0e38bc2d4ea34b7",
   167  	ProjectID:     "3a705b9f56bb439381b43c4fe59dccce",
   168  	ID:            "3229b3849f584faea483d6851f7aab05",
   169  	ResourceLimit: 5,
   170  	Description:   "Number of snapshots for project 3a705b9f56bb439381b43c4fe59dccce",
   171  }
   172  
   173  // ExpectedLimitsSlice is the slice of limits expected to be returned from ListOutput.
   174  var ExpectedLimitsSlice = []limits.Limit{FirstLimit, SecondLimit}
   175  
   176  // HandleGetEnforcementModelSuccessfully creates an HTTP handler at `/limits/model` on the
   177  // test handler mux that responds with a enforcement model.
   178  func HandleGetEnforcementModelSuccessfully(t *testing.T) {
   179  	th.Mux.HandleFunc("/limits/model", func(w http.ResponseWriter, r *http.Request) {
   180  		th.TestMethod(t, r, "GET")
   181  		th.TestHeader(t, r, "Accept", "application/json")
   182  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   183  
   184  		w.Header().Set("Content-Type", "application/json")
   185  		w.WriteHeader(http.StatusOK)
   186  		fmt.Fprintf(w, GetEnforcementModelOutput)
   187  	})
   188  }
   189  
   190  // HandleListLimitsSuccessfully creates an HTTP handler at `/limits` on the
   191  // test handler mux that responds with a list of two limits.
   192  func HandleListLimitsSuccessfully(t *testing.T) {
   193  	th.Mux.HandleFunc("/limits", func(w http.ResponseWriter, r *http.Request) {
   194  		th.TestMethod(t, r, "GET")
   195  		th.TestHeader(t, r, "Accept", "application/json")
   196  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   197  
   198  		w.Header().Set("Content-Type", "application/json")
   199  		w.WriteHeader(http.StatusOK)
   200  		fmt.Fprintf(w, ListOutput)
   201  	})
   202  }
   203  
   204  // HandleCreateLimitSuccessfully creates an HTTP handler at `/limits` on the
   205  // test handler mux that tests limit creation.
   206  func HandleCreateLimitSuccessfully(t *testing.T) {
   207  	th.Mux.HandleFunc("/limits", func(w http.ResponseWriter, r *http.Request) {
   208  		th.TestMethod(t, r, "POST")
   209  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   210  		th.TestJSONRequest(t, r, CreateRequest)
   211  
   212  		w.WriteHeader(http.StatusCreated)
   213  		fmt.Fprintf(w, CreateOutput)
   214  	})
   215  }
   216  
   217  // HandleGetLimitSuccessfully creates an HTTP handler at `/limits` on the
   218  // test handler mux that responds with a single limit.
   219  func HandleGetLimitSuccessfully(t *testing.T) {
   220  	th.Mux.HandleFunc("/limits/25a04c7a065c430590881c646cdcdd58", func(w http.ResponseWriter, r *http.Request) {
   221  		th.TestMethod(t, r, "GET")
   222  		th.TestHeader(t, r, "Accept", "application/json")
   223  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   224  
   225  		w.Header().Set("Content-Type", "application/json")
   226  		w.WriteHeader(http.StatusOK)
   227  		fmt.Fprintf(w, GetOutput)
   228  	})
   229  }
   230  
   231  // HandleUpdateLimitSuccessfully creates an HTTP handler at `/limits` on the
   232  // test handler mux that tests limit update.
   233  func HandleUpdateLimitSuccessfully(t *testing.T) {
   234  	th.Mux.HandleFunc("/limits/3229b3849f584faea483d6851f7aab05", func(w http.ResponseWriter, r *http.Request) {
   235  		th.TestMethod(t, r, "PATCH")
   236  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   237  		th.TestJSONRequest(t, r, UpdateRequest)
   238  
   239  		w.WriteHeader(http.StatusOK)
   240  		fmt.Fprintf(w, UpdateOutput)
   241  	})
   242  }
   243  
   244  // HandleDeleteLimitSuccessfully creates an HTTP handler at `/limits` on the
   245  // test handler mux that tests limit deletion.
   246  func HandleDeleteLimitSuccessfully(t *testing.T) {
   247  	th.Mux.HandleFunc("/limits/3229b3849f584faea483d6851f7aab05", func(w http.ResponseWriter, r *http.Request) {
   248  		th.TestMethod(t, r, "DELETE")
   249  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   250  
   251  		w.WriteHeader(http.StatusNoContent)
   252  	})
   253  }