1package testing 2 3import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/identity/v3/credentials" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11) 12 13const userID = "bb5476fd12884539b41d5a88f838d773" 14const credentialID = "3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510" 15const projectID = "731fc6f265cd486d900f16e84c5cb594" 16 17// ListOutput provides a single page of Credential results. 18const ListOutput = ` 19{ 20 "credentials": [ 21 { 22 "user_id": "bb5476fd12884539b41d5a88f838d773", 23 "links": { 24 "self": "http://identity/v3/credentials/3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510" 25 }, 26 "blob": "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 27 "project_id": "731fc6f265cd486d900f16e84c5cb594", 28 "type": "ec2", 29 "id": "3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510" 30 }, 31 { 32 "user_id": "6f556708d04b4ea6bc72d7df2296b71a", 33 "links": { 34 "self": "http://identity/v3/credentials/2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609" 35 }, 36 "blob": "{\"access\":\"7da79ff0aa364e1396f067e352b9b79a\",\"secret\":\"7a18d68ba8834b799d396f3ff6f1e98c\"}", 37 "project_id": "1a1d14690f3c4ec5bf5f321c5fde3c16", 38 "type": "ec2", 39 "id": "2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609" 40 } 41 ], 42 "links": { 43 "self": "http://identity/v3/credentials", 44 "previous": null, 45 "next": null 46 } 47} 48` 49 50// GetOutput provides a Get result. 51const GetOutput = ` 52{ 53 "credential": { 54 "user_id": "bb5476fd12884539b41d5a88f838d773", 55 "links": { 56 "self": "http://identity/v3/credentials/3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510" 57 }, 58 "blob": "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 59 "project_id": "731fc6f265cd486d900f16e84c5cb594", 60 "type": "ec2", 61 "id": "3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510" 62 } 63} 64` 65 66// CreateRequest provides the input to a Create request. 67const CreateRequest = ` 68{ 69 "credential": { 70 "blob": "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 71 "project_id": "731fc6f265cd486d900f16e84c5cb594", 72 "type": "ec2", 73 "user_id": "bb5476fd12884539b41d5a88f838d773" 74 } 75} 76` 77 78// UpdateRequest provides the input to as Update request. 79const UpdateRequest = ` 80{ 81 "credential": { 82 "blob": "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 83 "project_id": "731fc6f265cd486d900f16e84c5cb594", 84 "type": "ec2", 85 "user_id": "bb5476fd12884539b41d5a88f838d773" 86 } 87} 88` 89 90// UpdateOutput provides an update result. 91const UpdateOutput = ` 92{ 93 "credential": { 94 "user_id": "bb5476fd12884539b41d5a88f838d773", 95 "links": { 96 "self": "http://identity/v3/credentials/2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609" 97 }, 98 "blob": "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 99 "project_id": "731fc6f265cd486d900f16e84c5cb594", 100 "type": "ec2", 101 "id": "2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609" 102 } 103} 104` 105 106var Credential = credentials.Credential{ 107 ID: credentialID, 108 ProjectID: projectID, 109 Type: "ec2", 110 UserID: userID, 111 Blob: "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 112 Links: map[string]interface{}{ 113 "self": "http://identity/v3/credentials/3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510", 114 }, 115} 116 117var FirstCredential = credentials.Credential{ 118 ID: credentialID, 119 ProjectID: projectID, 120 Type: "ec2", 121 UserID: userID, 122 Blob: "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 123 Links: map[string]interface{}{ 124 "self": "http://identity/v3/credentials/3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510", 125 }, 126} 127 128var SecondCredential = credentials.Credential{ 129 ID: "2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609", 130 ProjectID: "1a1d14690f3c4ec5bf5f321c5fde3c16", 131 Type: "ec2", 132 UserID: "6f556708d04b4ea6bc72d7df2296b71a", 133 Blob: "{\"access\":\"7da79ff0aa364e1396f067e352b9b79a\",\"secret\":\"7a18d68ba8834b799d396f3ff6f1e98c\"}", 134 Links: map[string]interface{}{ 135 "self": "http://identity/v3/credentials/2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609", 136 }, 137} 138 139// SecondCredentialUpdated is how SecondCredential should look after an Update. 140var SecondCredentialUpdated = credentials.Credential{ 141 ID: "2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609", 142 ProjectID: projectID, 143 Type: "ec2", 144 UserID: userID, 145 Blob: "{\"access\":\"181920\",\"secret\":\"secretKey\"}", 146 Links: map[string]interface{}{ 147 "self": "http://identity/v3/credentials/2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609", 148 }, 149} 150 151// ExpectedCredentialsSlice is the slice of credentials expected to be returned from ListOutput. 152var ExpectedCredentialsSlice = []credentials.Credential{FirstCredential, SecondCredential} 153 154// HandleListCredentialsSuccessfully creates an HTTP handler at `/credentials` on the 155// test handler mux that responds with a list of two credentials. 156func HandleListCredentialsSuccessfully(t *testing.T) { 157 th.Mux.HandleFunc("/credentials", func(w http.ResponseWriter, r *http.Request) { 158 th.TestMethod(t, r, "GET") 159 th.TestHeader(t, r, "Accept", "application/json") 160 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 161 162 w.Header().Set("Content-Type", "application/json") 163 w.WriteHeader(http.StatusOK) 164 fmt.Fprintf(w, ListOutput) 165 }) 166} 167 168// HandleGetCredentialSuccessfully creates an HTTP handler at `/credentials` on the 169// test handler mux that responds with a single credential. 170func HandleGetCredentialSuccessfully(t *testing.T) { 171 th.Mux.HandleFunc("/credentials/3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510", func(w http.ResponseWriter, r *http.Request) { 172 th.TestMethod(t, r, "GET") 173 th.TestHeader(t, r, "Accept", "application/json") 174 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 175 176 w.Header().Set("Content-Type", "application/json") 177 w.WriteHeader(http.StatusOK) 178 fmt.Fprintf(w, GetOutput) 179 }) 180} 181 182// HandleCreateCredentialSuccessfully creates an HTTP handler at `/credentials` on the 183// test handler mux that tests credential creation. 184func HandleCreateCredentialSuccessfully(t *testing.T) { 185 th.Mux.HandleFunc("/credentials", func(w http.ResponseWriter, r *http.Request) { 186 th.TestMethod(t, r, "POST") 187 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 188 th.TestJSONRequest(t, r, CreateRequest) 189 190 w.WriteHeader(http.StatusCreated) 191 fmt.Fprintf(w, GetOutput) 192 }) 193} 194 195// HandleDeleteCredentialSuccessfully creates an HTTP handler at `/credentials` on the 196// test handler mux that tests credential deletion. 197func HandleDeleteCredentialSuccessfully(t *testing.T) { 198 th.Mux.HandleFunc("/credentials/3d3367228f9c7665266604462ec60029bcd83ad89614021a80b2eb879c572510", func(w http.ResponseWriter, r *http.Request) { 199 th.TestMethod(t, r, "DELETE") 200 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 201 202 w.WriteHeader(http.StatusNoContent) 203 }) 204} 205 206// HandleUpdateCredentialsSuccessfully creates an HTTP handler at `/credentials` on the 207// test handler mux that tests credentials update. 208func HandleUpdateCredentialSuccessfully(t *testing.T) { 209 th.Mux.HandleFunc("/credentials/2441494e52ab6d594a34d74586075cb299489bdd1e9389e3ab06467a4f460609", func(w http.ResponseWriter, r *http.Request) { 210 th.TestMethod(t, r, "PATCH") 211 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 212 th.TestJSONRequest(t, r, UpdateRequest) 213 214 w.WriteHeader(http.StatusOK) 215 fmt.Fprintf(w, UpdateOutput) 216 }) 217} 218