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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud/openstack/identity/v3/applicationcredentials"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  	"github.com/gophercloud/gophercloud/testhelper/client"
    12  )
    13  
    14  const userID = "2844b2a08be147a08ef58317d6471f1f"
    15  const applicationCredentialID = "f741662395b249c9b8acdebf1722c5ae"
    16  const accessRuleID = "07d719df00f349ef8de77d542edf010c"
    17  
    18  // ListOutput provides a single page of ApplicationCredential results.
    19  const ListOutput = `
    20  {
    21    "links": {
    22      "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials",
    23      "previous": null,
    24      "next": null
    25    },
    26    "application_credentials": [
    27      {
    28        "links": {
    29          "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/c4859fb437df4b87a51a8f5adcfb0bc7"
    30        },
    31        "description": null,
    32        "roles": [
    33          {
    34            "id": "31f87923ae4a4d119aa0b85dcdbeed13",
    35            "domain_id": null,
    36            "name": "compute_viewer"
    37          }
    38        ],
    39        "expires_at": null,
    40        "unrestricted": false,
    41        "project_id": "53c2b94f63fb4f43a21b92d119ce549f",
    42        "id": "c4859fb437df4b87a51a8f5adcfb0bc7",
    43        "name": "test1"
    44      },
    45      {
    46        "links": {
    47          "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/6b8cc7647da64166a4a3cc0c88ebbabb"
    48        },
    49        "description": null,
    50        "roles": [
    51          {
    52            "id": "31f87923ae4a4d119aa0b85dcdbeed13",
    53            "domain_id": null,
    54            "name": "compute_viewer"
    55          },
    56          {
    57            "id": "4494bc5bea1a4105ad7fbba6a7eb9ef4",
    58            "domain_id": null,
    59            "name": "network_viewer"
    60          }
    61        ],
    62        "expires_at": "2019-03-12T12:12:12.123456",
    63        "unrestricted": true,
    64        "project_id": "53c2b94f63fb4f43a21b92d119ce549f",
    65        "id": "6b8cc7647da64166a4a3cc0c88ebbabb",
    66        "name": "test2"
    67      }
    68    ]
    69  }
    70  `
    71  
    72  // GetOutput provides a Get result.
    73  const GetOutput = `
    74  {
    75    "application_credential": {
    76      "links": {
    77        "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/f741662395b249c9b8acdebf1722c5ae"
    78      },
    79      "description": null,
    80      "roles": [
    81        {
    82          "id": "31f87923ae4a4d119aa0b85dcdbeed13",
    83          "domain_id": null,
    84          "name": "compute_viewer"
    85        }
    86      ],
    87      "access_rules": [
    88        {
    89          "path": "/v2.0/metrics",
    90          "id": "07d719df00f349ef8de77d542edf010c",
    91          "service": "monitoring",
    92          "method": "GET"
    93        }
    94      ],
    95      "expires_at": null,
    96      "unrestricted": false,
    97      "project_id": "53c2b94f63fb4f43a21b92d119ce549f",
    98      "id": "f741662395b249c9b8acdebf1722c5ae",
    99      "name": "test"
   100    }
   101  }
   102  `
   103  
   104  // CreateRequest provides the input to a Create request.
   105  const CreateRequest = `
   106  {
   107    "application_credential": {
   108      "secret": "mysecret",
   109      "unrestricted": false,
   110      "roles": [
   111        {
   112          "id": "31f87923ae4a4d119aa0b85dcdbeed13"
   113        }
   114      ],
   115      "access_rules": [
   116        {
   117          "path": "/v2.0/metrics",
   118          "method": "GET",
   119          "service": "monitoring"
   120        }
   121      ],
   122      "name": "test"
   123    }
   124  }
   125  `
   126  
   127  const CreateResponse = `
   128  {
   129    "application_credential": {
   130      "links": {
   131        "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/f741662395b249c9b8acdebf1722c5ae"
   132      },
   133      "description": null,
   134      "roles": [
   135        {
   136          "id": "31f87923ae4a4d119aa0b85dcdbeed13",
   137          "domain_id": null,
   138          "name": "compute_viewer"
   139        }
   140      ],
   141      "access_rules": [
   142        {
   143          "path": "/v2.0/metrics",
   144          "id": "07d719df00f349ef8de77d542edf010c",
   145          "service": "monitoring",
   146          "method": "GET"
   147        }
   148      ],
   149      "expires_at": null,
   150      "secret": "mysecret",
   151      "unrestricted": false,
   152      "project_id": "53c2b94f63fb4f43a21b92d119ce549f",
   153      "id": "f741662395b249c9b8acdebf1722c5ae",
   154      "name": "test"
   155    }
   156  }
   157  `
   158  
   159  // CreateNoOptionsRequest provides the input to a Create request with no Secret.
   160  const CreateNoSecretRequest = `
   161  {
   162    "application_credential": {
   163      "unrestricted": false,
   164      "name": "test1",
   165      "roles": [
   166        {
   167          "id": "31f87923ae4a4d119aa0b85dcdbeed13"
   168        }
   169      ]
   170    }
   171  }
   172  `
   173  
   174  const CreateNoSecretResponse = `
   175  {
   176    "application_credential": {
   177      "links": {
   178        "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/c4859fb437df4b87a51a8f5adcfb0bc7"
   179      },
   180      "description": null,
   181      "roles": [
   182        {
   183          "id": "31f87923ae4a4d119aa0b85dcdbeed13",
   184          "domain_id": null,
   185          "name": "compute_viewer"
   186        }
   187      ],
   188      "expires_at": null,
   189      "secret": "generated_secret",
   190      "unrestricted": false,
   191      "project_id": "53c2b94f63fb4f43a21b92d119ce549f",
   192      "id": "c4859fb437df4b87a51a8f5adcfb0bc7",
   193      "name": "test1"
   194    }
   195  }
   196  `
   197  
   198  const CreateUnrestrictedRequest = `
   199  {
   200    "application_credential": {
   201      "unrestricted": true,
   202      "roles": [
   203        {
   204          "id": "31f87923ae4a4d119aa0b85dcdbeed13"
   205        },
   206        {
   207          "id": "4494bc5bea1a4105ad7fbba6a7eb9ef4"
   208        }
   209      ],
   210      "expires_at": "2019-03-12T12:12:12.123456",
   211      "name": "test2"
   212    }
   213  }
   214  `
   215  
   216  const CreateUnrestrictedResponse = `
   217  {
   218    "application_credential": {
   219      "links": {
   220        "self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/6b8cc7647da64166a4a3cc0c88ebbabb"
   221      },
   222      "description": null,
   223      "roles": [
   224        {
   225          "id": "31f87923ae4a4d119aa0b85dcdbeed13",
   226          "domain_id": null,
   227          "name": "compute_viewer"
   228        },
   229        {
   230          "id": "4494bc5bea1a4105ad7fbba6a7eb9ef4",
   231          "domain_id": null,
   232          "name": "network_viewer"
   233        }
   234      ],
   235      "expires_at": "2019-03-12T12:12:12.123456",
   236      "secret": "generated_secret",
   237      "unrestricted": true,
   238      "project_id": "53c2b94f63fb4f43a21b92d119ce549f",
   239      "id": "6b8cc7647da64166a4a3cc0c88ebbabb",
   240      "name": "test2"
   241    }
   242  }
   243  `
   244  
   245  // ListAccessRulesOutput provides a single page of AccessRules results.
   246  const ListAccessRulesOutput = `
   247  {
   248    "links": {
   249      "self": "https://example.com/identity/v3/users/2844b2a08be147a08ef58317d6471f1f/access_rules",
   250      "previous": null,
   251      "next": null
   252    },
   253    "access_rules": [
   254      {
   255        "path": "/v2.0/metrics",
   256        "links": {
   257          "self": "https://example.com/identity/v3/access_rules/07d719df00f349ef8de77d542edf010c"
   258        },
   259        "id": "07d719df00f349ef8de77d542edf010c",
   260        "service": "monitoring",
   261        "method": "GET"
   262      }
   263    ]
   264  }`
   265  
   266  // GetAccessRuleOutput provides a Get result.
   267  const GetAccessRuleOutput = `
   268  {
   269    "access_rule": {
   270      "path": "/v2.0/metrics",
   271      "links": {
   272        "self": "https://example.com/identity/v3/access_rules/07d719df00f349ef8de77d542edf010c"
   273      },
   274      "id": "07d719df00f349ef8de77d542edf010c",
   275      "service": "monitoring",
   276      "method": "GET"
   277    }
   278  }
   279  `
   280  
   281  var nilTime time.Time
   282  var ApplicationCredential = applicationcredentials.ApplicationCredential{
   283  	ID:           "f741662395b249c9b8acdebf1722c5ae",
   284  	Name:         "test",
   285  	Description:  "",
   286  	Unrestricted: false,
   287  	Secret:       "",
   288  	ProjectID:    "53c2b94f63fb4f43a21b92d119ce549f",
   289  	Roles: []applicationcredentials.Role{
   290  		{
   291  			ID:   "31f87923ae4a4d119aa0b85dcdbeed13",
   292  			Name: "compute_viewer",
   293  		},
   294  	},
   295  	AccessRules: []applicationcredentials.AccessRule{
   296  		{
   297  			ID:      "07d719df00f349ef8de77d542edf010c",
   298  			Path:    "/v2.0/metrics",
   299  			Method:  "GET",
   300  			Service: "monitoring",
   301  		},
   302  	},
   303  	ExpiresAt: nilTime,
   304  	Links: map[string]interface{}{
   305  		"self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/f741662395b249c9b8acdebf1722c5ae",
   306  	},
   307  }
   308  
   309  var ApplicationCredentialNoSecretResponse = applicationcredentials.ApplicationCredential{
   310  	ID:           "c4859fb437df4b87a51a8f5adcfb0bc7",
   311  	Name:         "test1",
   312  	Description:  "",
   313  	Unrestricted: false,
   314  	Secret:       "generated_secret",
   315  	ProjectID:    "53c2b94f63fb4f43a21b92d119ce549f",
   316  	Roles: []applicationcredentials.Role{
   317  		{
   318  			ID:   "31f87923ae4a4d119aa0b85dcdbeed13",
   319  			Name: "compute_viewer",
   320  		},
   321  	},
   322  	ExpiresAt: nilTime,
   323  	Links: map[string]interface{}{
   324  		"self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/c4859fb437df4b87a51a8f5adcfb0bc7",
   325  	},
   326  }
   327  
   328  var ApplationCredentialExpiresAt = time.Date(2019, 3, 12, 12, 12, 12, 123456000, time.UTC)
   329  var UnrestrictedApplicationCredential = applicationcredentials.ApplicationCredential{
   330  	ID:           "6b8cc7647da64166a4a3cc0c88ebbabb",
   331  	Name:         "test2",
   332  	Description:  "",
   333  	Unrestricted: true,
   334  	Secret:       "",
   335  	ProjectID:    "53c2b94f63fb4f43a21b92d119ce549f",
   336  	Roles: []applicationcredentials.Role{
   337  		{
   338  			ID:   "31f87923ae4a4d119aa0b85dcdbeed13",
   339  			Name: "compute_viewer",
   340  		},
   341  		{
   342  			ID:   "4494bc5bea1a4105ad7fbba6a7eb9ef4",
   343  			Name: "network_viewer",
   344  		},
   345  	},
   346  	ExpiresAt: ApplationCredentialExpiresAt,
   347  	Links: map[string]interface{}{
   348  		"self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/6b8cc7647da64166a4a3cc0c88ebbabb",
   349  	},
   350  }
   351  
   352  var FirstApplicationCredential = applicationcredentials.ApplicationCredential{
   353  	ID:           "c4859fb437df4b87a51a8f5adcfb0bc7",
   354  	Name:         "test1",
   355  	Description:  "",
   356  	Unrestricted: false,
   357  	Secret:       "",
   358  	ProjectID:    "53c2b94f63fb4f43a21b92d119ce549f",
   359  	Roles: []applicationcredentials.Role{
   360  		{
   361  			ID:   "31f87923ae4a4d119aa0b85dcdbeed13",
   362  			Name: "compute_viewer",
   363  		},
   364  	},
   365  	ExpiresAt: nilTime,
   366  	Links: map[string]interface{}{
   367  		"self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/c4859fb437df4b87a51a8f5adcfb0bc7",
   368  	},
   369  }
   370  
   371  var SecondApplicationCredential = applicationcredentials.ApplicationCredential{
   372  	ID:           "6b8cc7647da64166a4a3cc0c88ebbabb",
   373  	Name:         "test2",
   374  	Description:  "",
   375  	Unrestricted: true,
   376  	Secret:       "",
   377  	ProjectID:    "53c2b94f63fb4f43a21b92d119ce549f",
   378  	Roles: []applicationcredentials.Role{
   379  		{
   380  			ID:   "31f87923ae4a4d119aa0b85dcdbeed13",
   381  			Name: "compute_viewer",
   382  		},
   383  		{
   384  			ID:   "4494bc5bea1a4105ad7fbba6a7eb9ef4",
   385  			Name: "network_viewer",
   386  		},
   387  	},
   388  	ExpiresAt: ApplationCredentialExpiresAt,
   389  	Links: map[string]interface{}{
   390  		"self": "https://identity/v3/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/6b8cc7647da64166a4a3cc0c88ebbabb",
   391  	},
   392  }
   393  
   394  var AccessRule = applicationcredentials.AccessRule{
   395  	Path:    "/v2.0/metrics",
   396  	ID:      "07d719df00f349ef8de77d542edf010c",
   397  	Service: "monitoring",
   398  	Method:  "GET",
   399  }
   400  
   401  var ExpectedAccessRulesSlice = []applicationcredentials.AccessRule{
   402  	AccessRule,
   403  }
   404  
   405  // ExpectedApplicationCredentialsSlice is the slice of application credentials expected to be returned from ListOutput.
   406  var ExpectedApplicationCredentialsSlice = []applicationcredentials.ApplicationCredential{FirstApplicationCredential, SecondApplicationCredential}
   407  
   408  // HandleListApplicationCredentialsSuccessfully creates an HTTP handler at `/users` on the
   409  // test handler mux that responds with a list of two applicationcredentials.
   410  func HandleListApplicationCredentialsSuccessfully(t *testing.T) {
   411  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/application_credentials", func(w http.ResponseWriter, r *http.Request) {
   412  		th.TestMethod(t, r, "GET")
   413  		th.TestHeader(t, r, "Accept", "application/json")
   414  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   415  
   416  		w.Header().Set("Content-Type", "application/json")
   417  		w.WriteHeader(http.StatusOK)
   418  		fmt.Fprintf(w, ListOutput)
   419  	})
   420  }
   421  
   422  // HandleGetApplicationCredentialSuccessfully creates an HTTP handler at `/users` on the
   423  // test handler mux that responds with a single application credential.
   424  func HandleGetApplicationCredentialSuccessfully(t *testing.T) {
   425  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/f741662395b249c9b8acdebf1722c5ae", func(w http.ResponseWriter, r *http.Request) {
   426  		th.TestMethod(t, r, "GET")
   427  		th.TestHeader(t, r, "Accept", "application/json")
   428  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   429  
   430  		w.Header().Set("Content-Type", "application/json")
   431  		w.WriteHeader(http.StatusOK)
   432  		fmt.Fprintf(w, GetOutput)
   433  	})
   434  }
   435  
   436  // HandleCreateApplicationCredentialSuccessfully creates an HTTP handler at `/users` on the
   437  // test handler mux that tests application credential creation.
   438  func HandleCreateApplicationCredentialSuccessfully(t *testing.T) {
   439  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/application_credentials", func(w http.ResponseWriter, r *http.Request) {
   440  		th.TestMethod(t, r, "POST")
   441  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   442  		th.TestJSONRequest(t, r, CreateRequest)
   443  
   444  		w.WriteHeader(http.StatusCreated)
   445  		fmt.Fprintf(w, CreateResponse)
   446  	})
   447  }
   448  
   449  // HandleCreateNoOptionsApplicationCredentialSuccessfully creates an HTTP handler at `/users` on the
   450  // test handler mux that tests application credential creation.
   451  func HandleCreateNoSecretApplicationCredentialSuccessfully(t *testing.T) {
   452  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/application_credentials", func(w http.ResponseWriter, r *http.Request) {
   453  		th.TestMethod(t, r, "POST")
   454  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   455  		th.TestJSONRequest(t, r, CreateNoSecretRequest)
   456  
   457  		w.WriteHeader(http.StatusCreated)
   458  		fmt.Fprintf(w, CreateNoSecretResponse)
   459  	})
   460  }
   461  
   462  func HandleCreateUnrestrictedApplicationCredentialSuccessfully(t *testing.T) {
   463  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/application_credentials", func(w http.ResponseWriter, r *http.Request) {
   464  		th.TestMethod(t, r, "POST")
   465  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   466  		th.TestJSONRequest(t, r, CreateUnrestrictedRequest)
   467  
   468  		w.WriteHeader(http.StatusCreated)
   469  		fmt.Fprintf(w, CreateUnrestrictedResponse)
   470  	})
   471  }
   472  
   473  // HandleDeleteApplicationCredentialSuccessfully creates an HTTP handler at `/users` on the
   474  // test handler mux that tests application credential deletion.
   475  func HandleDeleteApplicationCredentialSuccessfully(t *testing.T) {
   476  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/application_credentials/f741662395b249c9b8acdebf1722c5ae", func(w http.ResponseWriter, r *http.Request) {
   477  		th.TestMethod(t, r, "DELETE")
   478  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   479  
   480  		w.WriteHeader(http.StatusNoContent)
   481  	})
   482  }
   483  
   484  // HandleListAccessRulesSuccessfully creates an HTTP handler at `/users` on the
   485  // test handler mux that responds with a list of two applicationcredentials.
   486  func HandleListAccessRulesSuccessfully(t *testing.T) {
   487  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/access_rules", func(w http.ResponseWriter, r *http.Request) {
   488  		th.TestMethod(t, r, "GET")
   489  		th.TestHeader(t, r, "Accept", "application/json")
   490  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   491  
   492  		w.Header().Set("Content-Type", "application/json")
   493  		w.WriteHeader(http.StatusOK)
   494  		fmt.Fprintf(w, ListAccessRulesOutput)
   495  	})
   496  }
   497  
   498  // HandleGetAccessRuleSuccessfully creates an HTTP handler at `/users` on the
   499  // test handler mux that responds with a single application credential.
   500  func HandleGetAccessRuleSuccessfully(t *testing.T) {
   501  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/access_rules/07d719df00f349ef8de77d542edf010c", func(w http.ResponseWriter, r *http.Request) {
   502  		th.TestMethod(t, r, "GET")
   503  		th.TestHeader(t, r, "Accept", "application/json")
   504  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   505  
   506  		w.Header().Set("Content-Type", "application/json")
   507  		w.WriteHeader(http.StatusOK)
   508  		fmt.Fprintf(w, GetAccessRuleOutput)
   509  	})
   510  }
   511  
   512  // HandleDeleteAccessRuleSuccessfully creates an HTTP handler at `/users` on the
   513  // test handler mux that tests application credential deletion.
   514  func HandleDeleteAccessRuleSuccessfully(t *testing.T) {
   515  	th.Mux.HandleFunc("/users/2844b2a08be147a08ef58317d6471f1f/access_rules/07d719df00f349ef8de77d542edf010c", func(w http.ResponseWriter, r *http.Request) {
   516  		th.TestMethod(t, r, "DELETE")
   517  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   518  
   519  		w.WriteHeader(http.StatusNoContent)
   520  	})
   521  }