github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/roles/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/roles"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	fake "github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  // ListOutput provides a single page of Role results.
    14  const ListOutput = `
    15  {
    16      "links": {
    17          "next": null,
    18          "previous": null,
    19          "self": "http://example.com/identity/v3/roles"
    20      },
    21      "roles": [
    22          {
    23              "domain_id": "default",
    24              "id": "2844b2a08be147a08ef58317d6471f1f",
    25              "links": {
    26                  "self": "http://example.com/identity/v3/roles/2844b2a08be147a08ef58317d6471f1f"
    27              },
    28              "name": "admin-read-only"
    29          },
    30          {
    31              "domain_id": "1789d1",
    32              "id": "9fe1d3",
    33              "links": {
    34                  "self": "https://example.com/identity/v3/roles/9fe1d3"
    35              },
    36              "name": "support",
    37              "extra": {
    38                  "description": "read-only support role"
    39              }
    40          }
    41      ]
    42  }
    43  `
    44  
    45  // GetOutput provides a Get result.
    46  const GetOutput = `
    47  {
    48      "role": {
    49          "domain_id": "1789d1",
    50          "id": "9fe1d3",
    51          "links": {
    52              "self": "https://example.com/identity/v3/roles/9fe1d3"
    53          },
    54          "name": "support",
    55          "extra": {
    56              "description": "read-only support role"
    57          }
    58      }
    59  }
    60  `
    61  
    62  // CreateRequest provides the input to a Create request.
    63  const CreateRequest = `
    64  {
    65      "role": {
    66          "domain_id": "1789d1",
    67          "name": "support",
    68          "description": "read-only support role"
    69      }
    70  }
    71  `
    72  
    73  // UpdateRequest provides the input to as Update request.
    74  const UpdateRequest = `
    75  {
    76      "role": {
    77          "description": "admin read-only support role"
    78      }
    79  }
    80  `
    81  
    82  // UpdateOutput provides an update result.
    83  const UpdateOutput = `
    84  {
    85      "role": {
    86          "domain_id": "1789d1",
    87          "id": "9fe1d3",
    88          "links": {
    89              "self": "https://example.com/identity/v3/roles/9fe1d3"
    90          },
    91          "name": "support",
    92          "extra": {
    93              "description": "admin read-only support role"
    94          }
    95      }
    96  }
    97  `
    98  
    99  // ListAssignmentOutput provides a result of ListAssignment request.
   100  const ListAssignmentOutput = `
   101  {
   102      "role_assignments": [
   103          {
   104              "links": {
   105                  "assignment": "http://identity:35357/v3/domains/161718/users/313233/roles/123456"
   106              },
   107              "role": {
   108                  "id": "123456"
   109              },
   110              "scope": {
   111                  "domain": {
   112                      "id": "161718"
   113                  }
   114              },
   115              "user": {
   116                  "domain": {
   117                    "id": "161718"
   118                  },
   119                  "id": "313233"
   120              }
   121          },
   122          {
   123              "links": {
   124                  "assignment": "http://identity:35357/v3/projects/456789/groups/101112/roles/123456",
   125                  "membership": "http://identity:35357/v3/groups/101112/users/313233"
   126              },
   127              "role": {
   128                  "id": "123456"
   129              },
   130              "scope": {
   131                  "project": {
   132                      "domain": {
   133                        "id": "161718"
   134                      },
   135                      "id": "456789"
   136                  }
   137              },
   138              "user": {
   139                  "domain": {
   140                    "id": "161718"
   141                  },
   142                  "id": "313233"
   143              }
   144          }
   145      ],
   146      "links": {
   147          "self": "http://identity:35357/v3/role_assignments?effective",
   148          "previous": null,
   149          "next": null
   150      }
   151  }
   152  `
   153  
   154  // ListAssignmentWithNamesOutput provides a result of ListAssignment request with IncludeNames option.
   155  const ListAssignmentWithNamesOutput = `
   156  {
   157      "role_assignments": [
   158          {
   159              "links": {
   160                  "assignment": "http://identity:35357/v3/domains/161718/users/313233/roles/123456"
   161              },
   162              "role": {
   163                  "id": "123456",
   164                  "name": "include_names_role"
   165              },
   166              "scope": {
   167                  "domain": {
   168                      "id": "161718",
   169                      "name": "52833"
   170                  }
   171              },
   172              "user": {
   173                  "domain": {
   174                      "id": "161718",
   175                      "name": "52833"
   176                  },
   177                  "id": "313233",
   178                  "name": "example-user-name"
   179              }
   180          }
   181      ],
   182      "links": {
   183          "self": "http://identity:35357/v3/role_assignments?include_names=True",
   184          "previous": null,
   185          "next": null
   186      }
   187  }
   188  `
   189  
   190  // ListAssignmentsOnResourceOutput provides a result of ListAssignmentsOnResource request.
   191  const ListAssignmentsOnResourceOutput = `
   192  {
   193      "links": {
   194          "next": null,
   195          "previous": null,
   196          "self": "http://example.com/identity/v3/projects/9e5a15/users/b964a9/roles"
   197      },
   198      "roles": [
   199          {
   200              "id": "9fe1d3",
   201              "links": {
   202                  "self": "https://example.com/identity/v3/roles/9fe1d3"
   203              },
   204              "name": "support",
   205              "extra": {
   206                  "description": "read-only support role"
   207              }
   208          }
   209      ]
   210  }
   211  `
   212  
   213  // FirstRole is the first role in the List request.
   214  var FirstRole = roles.Role{
   215  	DomainID: "default",
   216  	ID:       "2844b2a08be147a08ef58317d6471f1f",
   217  	Links: map[string]interface{}{
   218  		"self": "http://example.com/identity/v3/roles/2844b2a08be147a08ef58317d6471f1f",
   219  	},
   220  	Name:  "admin-read-only",
   221  	Extra: map[string]interface{}{},
   222  }
   223  
   224  // SecondRole is the second role in the List request.
   225  var SecondRole = roles.Role{
   226  	DomainID: "1789d1",
   227  	ID:       "9fe1d3",
   228  	Links: map[string]interface{}{
   229  		"self": "https://example.com/identity/v3/roles/9fe1d3",
   230  	},
   231  	Name: "support",
   232  	Extra: map[string]interface{}{
   233  		"description": "read-only support role",
   234  	},
   235  }
   236  
   237  // SecondRoleUpdated is how SecondRole should look after an Update.
   238  var SecondRoleUpdated = roles.Role{
   239  	DomainID: "1789d1",
   240  	ID:       "9fe1d3",
   241  	Links: map[string]interface{}{
   242  		"self": "https://example.com/identity/v3/roles/9fe1d3",
   243  	},
   244  	Name: "support",
   245  	Extra: map[string]interface{}{
   246  		"description": "admin read-only support role",
   247  	},
   248  }
   249  
   250  // ExpectedRolesSlice is the slice of roles expected to be returned from ListOutput.
   251  var ExpectedRolesSlice = []roles.Role{FirstRole, SecondRole}
   252  
   253  // HandleListRolesSuccessfully creates an HTTP handler at `/roles` on the
   254  // test handler mux that responds with a list of two roles.
   255  func HandleListRolesSuccessfully(t *testing.T) {
   256  	th.Mux.HandleFunc("/roles", func(w http.ResponseWriter, r *http.Request) {
   257  		th.TestMethod(t, r, "GET")
   258  		th.TestHeader(t, r, "Accept", "application/json")
   259  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   260  
   261  		w.Header().Set("Content-Type", "application/json")
   262  		w.WriteHeader(http.StatusOK)
   263  		fmt.Fprintf(w, ListOutput)
   264  	})
   265  }
   266  
   267  // HandleGetRoleSuccessfully creates an HTTP handler at `/roles` on the
   268  // test handler mux that responds with a single role.
   269  func HandleGetRoleSuccessfully(t *testing.T) {
   270  	th.Mux.HandleFunc("/roles/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
   271  		th.TestMethod(t, r, "GET")
   272  		th.TestHeader(t, r, "Accept", "application/json")
   273  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   274  
   275  		w.Header().Set("Content-Type", "application/json")
   276  		w.WriteHeader(http.StatusOK)
   277  		fmt.Fprintf(w, GetOutput)
   278  	})
   279  }
   280  
   281  // HandleCreateRoleSuccessfully creates an HTTP handler at `/roles` on the
   282  // test handler mux that tests role creation.
   283  func HandleCreateRoleSuccessfully(t *testing.T) {
   284  	th.Mux.HandleFunc("/roles", func(w http.ResponseWriter, r *http.Request) {
   285  		th.TestMethod(t, r, "POST")
   286  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   287  		th.TestJSONRequest(t, r, CreateRequest)
   288  
   289  		w.WriteHeader(http.StatusCreated)
   290  		fmt.Fprintf(w, GetOutput)
   291  	})
   292  }
   293  
   294  // HandleUpdateRoleSuccessfully creates an HTTP handler at `/roles` on the
   295  // test handler mux that tests role update.
   296  func HandleUpdateRoleSuccessfully(t *testing.T) {
   297  	th.Mux.HandleFunc("/roles/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
   298  		th.TestMethod(t, r, "PATCH")
   299  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   300  		th.TestJSONRequest(t, r, UpdateRequest)
   301  
   302  		w.WriteHeader(http.StatusOK)
   303  		fmt.Fprintf(w, UpdateOutput)
   304  	})
   305  }
   306  
   307  // HandleDeleteRoleSuccessfully creates an HTTP handler at `/roles` on the
   308  // test handler mux that tests role deletion.
   309  func HandleDeleteRoleSuccessfully(t *testing.T) {
   310  	th.Mux.HandleFunc("/roles/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
   311  		th.TestMethod(t, r, "DELETE")
   312  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   313  
   314  		w.WriteHeader(http.StatusNoContent)
   315  	})
   316  }
   317  
   318  func HandleAssignSuccessfully(t *testing.T) {
   319  	th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   320  		th.TestMethod(t, r, "PUT")
   321  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   322  		w.WriteHeader(http.StatusNoContent)
   323  	})
   324  
   325  	th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   326  		th.TestMethod(t, r, "PUT")
   327  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   328  		w.WriteHeader(http.StatusNoContent)
   329  	})
   330  
   331  	th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   332  		th.TestMethod(t, r, "PUT")
   333  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   334  		w.WriteHeader(http.StatusNoContent)
   335  	})
   336  
   337  	th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   338  		th.TestMethod(t, r, "PUT")
   339  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   340  		w.WriteHeader(http.StatusNoContent)
   341  	})
   342  }
   343  
   344  func HandleUnassignSuccessfully(t *testing.T) {
   345  	th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   346  		th.TestMethod(t, r, "DELETE")
   347  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   348  		w.WriteHeader(http.StatusNoContent)
   349  	})
   350  
   351  	th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   352  		th.TestMethod(t, r, "DELETE")
   353  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   354  		w.WriteHeader(http.StatusNoContent)
   355  	})
   356  
   357  	th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   358  		th.TestMethod(t, r, "DELETE")
   359  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   360  		w.WriteHeader(http.StatusNoContent)
   361  	})
   362  
   363  	th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) {
   364  		th.TestMethod(t, r, "DELETE")
   365  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   366  		w.WriteHeader(http.StatusNoContent)
   367  	})
   368  }
   369  
   370  // FirstRoleAssignment is the first role assignment in the List request.
   371  var FirstRoleAssignment = roles.RoleAssignment{
   372  	Role:  roles.AssignedRole{ID: "123456"},
   373  	Scope: roles.Scope{Domain: roles.Domain{ID: "161718"}},
   374  	User:  roles.User{Domain: roles.Domain{ID: "161718"}, ID: "313233"},
   375  	Group: roles.Group{},
   376  }
   377  
   378  // SecondRoleAssignemnt is the second role assignemnt in the List request.
   379  var SecondRoleAssignment = roles.RoleAssignment{
   380  	Role:  roles.AssignedRole{ID: "123456"},
   381  	Scope: roles.Scope{Project: roles.Project{Domain: roles.Domain{ID: "161718"}, ID: "456789"}},
   382  	User:  roles.User{Domain: roles.Domain{ID: "161718"}, ID: "313233"},
   383  	Group: roles.Group{},
   384  }
   385  
   386  // ThirdRoleAssignment is the third role assignment that has entity names in the List request.
   387  var ThirdRoleAssignment = roles.RoleAssignment{
   388  	Role:  roles.AssignedRole{ID: "123456", Name: "include_names_role"},
   389  	Scope: roles.Scope{Domain: roles.Domain{ID: "161718", Name: "52833"}},
   390  	User:  roles.User{Domain: roles.Domain{ID: "161718", Name: "52833"}, ID: "313233", Name: "example-user-name"},
   391  	Group: roles.Group{},
   392  }
   393  
   394  // ExpectedRoleAssignmentsSlice is the slice of role assignments expected to be
   395  // returned from ListAssignmentOutput.
   396  var ExpectedRoleAssignmentsSlice = []roles.RoleAssignment{FirstRoleAssignment, SecondRoleAssignment}
   397  
   398  // ExpectedRoleAssignmentsWithNamesSlice is the slice of role assignments expected to be
   399  // returned from ListAssignmentWithNamesOutput.
   400  var ExpectedRoleAssignmentsWithNamesSlice = []roles.RoleAssignment{ThirdRoleAssignment}
   401  
   402  // HandleListRoleAssignmentsSuccessfully creates an HTTP handler at `/role_assignments` on the
   403  // test handler mux that responds with a list of two role assignments.
   404  func HandleListRoleAssignmentsSuccessfully(t *testing.T) {
   405  	th.Mux.HandleFunc("/role_assignments", func(w http.ResponseWriter, r *http.Request) {
   406  		th.TestMethod(t, r, "GET")
   407  		th.TestHeader(t, r, "Accept", "application/json")
   408  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   409  
   410  		w.Header().Set("Content-Type", "application/json")
   411  		w.WriteHeader(http.StatusOK)
   412  		fmt.Fprintf(w, ListAssignmentOutput)
   413  	})
   414  }
   415  
   416  // HandleListRoleAssignmentsSuccessfully creates an HTTP handler at `/role_assignments` on the
   417  // test handler mux that responds with a list of two role assignments.
   418  func HandleListRoleAssignmentsWithNamesSuccessfully(t *testing.T) {
   419  	th.Mux.HandleFunc("/role_assignments", func(w http.ResponseWriter, r *http.Request) {
   420  		th.TestMethod(t, r, "GET")
   421  		th.TestHeader(t, r, "Accept", "application/json")
   422  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   423  		th.AssertEquals(t, "include_names=true", r.URL.RawQuery)
   424  
   425  		w.Header().Set("Content-Type", "application/json")
   426  		w.WriteHeader(http.StatusOK)
   427  		fmt.Fprintf(w, ListAssignmentWithNamesOutput)
   428  	})
   429  }
   430  
   431  // HandleListRoleAssignmentsWithSubtreeSuccessfully creates an HTTP handler at `/role_assignments` on the
   432  // test handler mux that responds with a list of two role assignments.
   433  func HandleListRoleAssignmentsWithSubtreeSuccessfully(t *testing.T) {
   434  	th.Mux.HandleFunc("/role_assignments", func(w http.ResponseWriter, r *http.Request) {
   435  		th.TestMethod(t, r, "GET")
   436  		th.TestHeader(t, r, "Accept", "application/json")
   437  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   438  		th.AssertEquals(t, "include_subtree=true", r.URL.RawQuery)
   439  
   440  		w.Header().Set("Content-Type", "application/json")
   441  		w.WriteHeader(http.StatusOK)
   442  		fmt.Fprintf(w, ListAssignmentOutput)
   443  	})
   444  }
   445  
   446  // RoleOnResource is the role in the ListAssignmentsOnResource request.
   447  var RoleOnResource = roles.Role{
   448  	ID: "9fe1d3",
   449  	Links: map[string]interface{}{
   450  		"self": "https://example.com/identity/v3/roles/9fe1d3",
   451  	},
   452  	Name: "support",
   453  	Extra: map[string]interface{}{
   454  		"description": "read-only support role",
   455  	},
   456  }
   457  
   458  // ExpectedRolesOnResourceSlice is the slice of roles expected to be returned
   459  // from ListAssignmentsOnResourceOutput.
   460  var ExpectedRolesOnResourceSlice = []roles.Role{RoleOnResource}
   461  
   462  func HandleListAssignmentsOnResourceSuccessfully_ProjectsUsers(t *testing.T) {
   463  	fn := func(w http.ResponseWriter, r *http.Request) {
   464  		th.TestMethod(t, r, "GET")
   465  		th.TestHeader(t, r, "Accept", "application/json")
   466  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   467  
   468  		w.Header().Set("Content-Type", "application/json")
   469  		w.WriteHeader(http.StatusOK)
   470  		fmt.Fprintf(w, ListAssignmentsOnResourceOutput)
   471  	}
   472  
   473  	th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles", fn)
   474  }
   475  
   476  func HandleListAssignmentsOnResourceSuccessfully_ProjectsGroups(t *testing.T) {
   477  	fn := func(w http.ResponseWriter, r *http.Request) {
   478  		th.TestMethod(t, r, "GET")
   479  		th.TestHeader(t, r, "Accept", "application/json")
   480  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   481  
   482  		w.Header().Set("Content-Type", "application/json")
   483  		w.WriteHeader(http.StatusOK)
   484  		fmt.Fprintf(w, ListAssignmentsOnResourceOutput)
   485  	}
   486  
   487  	th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles", fn)
   488  }
   489  
   490  func HandleListAssignmentsOnResourceSuccessfully_DomainsUsers(t *testing.T) {
   491  	fn := func(w http.ResponseWriter, r *http.Request) {
   492  		th.TestMethod(t, r, "GET")
   493  		th.TestHeader(t, r, "Accept", "application/json")
   494  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   495  
   496  		w.Header().Set("Content-Type", "application/json")
   497  		w.WriteHeader(http.StatusOK)
   498  		fmt.Fprintf(w, ListAssignmentsOnResourceOutput)
   499  	}
   500  
   501  	th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles", fn)
   502  }
   503  
   504  func HandleListAssignmentsOnResourceSuccessfully_DomainsGroups(t *testing.T) {
   505  	fn := func(w http.ResponseWriter, r *http.Request) {
   506  		th.TestMethod(t, r, "GET")
   507  		th.TestHeader(t, r, "Accept", "application/json")
   508  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   509  
   510  		w.Header().Set("Content-Type", "application/json")
   511  		w.WriteHeader(http.StatusOK)
   512  		fmt.Fprintf(w, ListAssignmentsOnResourceOutput)
   513  	}
   514  
   515  	th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles", fn)
   516  }