|
| 1 | +/* |
| 2 | + * Copyright 2018 - Present Okta, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/okta/okta-sdk-golang/okta.v2" |
| 25 | + "github.com/okta/okta-sdk-golang/tests.v2" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func Test_can_create_an_authorizaiton_server(t *testing.T) { |
| 31 | + client, _ := tests.NewClient() |
| 32 | + |
| 33 | + as := okta.AuthorizationServer{ |
| 34 | + Name: "Sample Authorizaiton Server - Golang", |
| 35 | + Description: "Sample Authorizaiton Server Description for Golang", |
| 36 | + Audiences: []string{"api://default"}, |
| 37 | + } |
| 38 | + |
| 39 | + authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(as) |
| 40 | + require.NoError(t, err, "creating an authorizaiton server should not error") |
| 41 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers") |
| 42 | + |
| 43 | + assert_authorization_server_model(t, authorizationServer) |
| 44 | + |
| 45 | + _, err = client.AuthorizationServer.DeleteAuthorizationServer(authorizationServer.Id) |
| 46 | +} |
| 47 | + |
| 48 | +func Test_can_get_an_authorizaiton_server(t *testing.T) { |
| 49 | + client, _ := tests.NewClient() |
| 50 | + |
| 51 | + as := okta.AuthorizationServer{ |
| 52 | + Name: "Sample Authorizaiton Server - Golang", |
| 53 | + Description: "Sample Authorizaiton Server Description for Golang", |
| 54 | + Audiences: []string{"api://default"}, |
| 55 | + } |
| 56 | + |
| 57 | + authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(as) |
| 58 | + require.NoError(t, err, "creating an authorizaiton server should not error") |
| 59 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers") |
| 60 | + |
| 61 | + authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id) |
| 62 | + require.NoError(t, err, "getting an authorizaiton server should not error") |
| 63 | + tests.Assert_response(t, response, "GET", "/api/v1/authorizationServers/"+authorizationServer.Id) |
| 64 | + |
| 65 | + assert_authorization_server_model(t, authorizationServer) |
| 66 | + |
| 67 | + assert.Equal(t, as.Name, authorizationServer.Name, "did not return the same authorization server name") |
| 68 | + assert.Equal(t, as.Description, authorizationServer.Description, "did not return the same authorization server description") |
| 69 | + |
| 70 | + _, err = client.AuthorizationServer.DeleteAuthorizationServer(authorizationServer.Id) |
| 71 | +} |
| 72 | + |
| 73 | +func Test_can_update_an_authorizaiton_server(t *testing.T) { |
| 74 | + client, _ := tests.NewClient() |
| 75 | + |
| 76 | + as := okta.AuthorizationServer{ |
| 77 | + Name: "Sample Authorizaiton Server - Golang", |
| 78 | + Description: "Sample Authorizaiton Server Description for Golang", |
| 79 | + Audiences: []string{"api://default"}, |
| 80 | + } |
| 81 | + |
| 82 | + authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(as) |
| 83 | + require.NoError(t, err, "creating an authorizaiton server should not error") |
| 84 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers") |
| 85 | + |
| 86 | + authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id) |
| 87 | + require.NoError(t, err, "getting an authorizaiton server should not error") |
| 88 | + tests.Assert_response(t, response, "GET", "/api/v1/authorizationServers/"+authorizationServer.Id) |
| 89 | + |
| 90 | + assert_authorization_server_model(t, authorizationServer) |
| 91 | + |
| 92 | + assert.Equal(t, as.Name, authorizationServer.Name, "did not return the same authorization server name") |
| 93 | + assert.Equal(t, as.Description, authorizationServer.Description, "did not return the same authorization server description") |
| 94 | + |
| 95 | + updatedName := "Updated Authorization Server - Golang" |
| 96 | + updatedDescription := "Updated Authorization Server Description" |
| 97 | + authorizationServer.Name = updatedName |
| 98 | + authorizationServer.Description = updatedDescription |
| 99 | + |
| 100 | + authorizationServer, response, err = client.AuthorizationServer.UpdateAuthorizationServer(authorizationServer.Id, *authorizationServer) |
| 101 | + require.NoError(t, err, "getting an authorizaiton server should not error") |
| 102 | + tests.Assert_response(t, response, "PUT", "/api/v1/authorizationServers/"+authorizationServer.Id) |
| 103 | + |
| 104 | + assert_authorization_server_model(t, authorizationServer) |
| 105 | + |
| 106 | + assert.Equal(t, updatedName, authorizationServer.Name, "did not return the same authorization server name") |
| 107 | + assert.Equal(t, updatedDescription, authorizationServer.Description, "did not return the same authorization server description") |
| 108 | + |
| 109 | + _, err = client.AuthorizationServer.DeleteAuthorizationServer(authorizationServer.Id) |
| 110 | +} |
| 111 | + |
| 112 | +func Test_can_delete_an_authorizaiton_server(t *testing.T) { |
| 113 | + client, _ := tests.NewClient() |
| 114 | + |
| 115 | + as := okta.AuthorizationServer{ |
| 116 | + Name: "Sample Authorizaiton Server - Golang", |
| 117 | + Description: "Sample Authorizaiton Server Description for Golang", |
| 118 | + Audiences: []string{"api://default"}, |
| 119 | + } |
| 120 | + |
| 121 | + authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(as) |
| 122 | + require.NoError(t, err, "creating an authorizaiton server should not error") |
| 123 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers") |
| 124 | + |
| 125 | + authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id) |
| 126 | + require.NoError(t, err, "getting an authorizaiton server should not error") |
| 127 | + tests.Assert_response(t, response, "GET", "/api/v1/authorizationServers/"+authorizationServer.Id) |
| 128 | + |
| 129 | + assert_authorization_server_model(t, authorizationServer) |
| 130 | + |
| 131 | + assert.Equal(t, as.Name, authorizationServer.Name, "did not return the same authorization server name") |
| 132 | + assert.Equal(t, as.Description, authorizationServer.Description, "did not return the same authorization server description") |
| 133 | + |
| 134 | + response, err = client.AuthorizationServer.DeleteAuthorizationServer(authorizationServer.Id) |
| 135 | + assert.Equal(t, http.StatusNoContent, response.StatusCode, "did not return a 204 status code during delete") |
| 136 | + |
| 137 | + authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id) |
| 138 | + assert.Error(t, err, "Finding an authorization server by id should have reported an error") |
| 139 | + assert.Equal(t, http.StatusNotFound, response.StatusCode, "Should have resulted in a 404 when finding a deleted authorization server") |
| 140 | +} |
| 141 | + |
| 142 | +func Test_can_list_authorizaiton_servers(t *testing.T) { |
| 143 | + client, _ := tests.NewClient() |
| 144 | + |
| 145 | + as := okta.AuthorizationServer{ |
| 146 | + Name: "Sample Authorizaiton Server - Golang", |
| 147 | + Description: "Sample Authorizaiton Server Description for Golang", |
| 148 | + Audiences: []string{"api://default"}, |
| 149 | + } |
| 150 | + |
| 151 | + authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(as) |
| 152 | + require.NoError(t, err, "creating an authorizaiton server should not error") |
| 153 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers") |
| 154 | + |
| 155 | + authorizationServerList, response, err := client.AuthorizationServer.ListAuthorizationServers(nil) |
| 156 | + require.NoError(t, err, "list authorizaiton servers should not error") |
| 157 | + tests.Assert_response(t, response, "GET", "/api/v1/authorizationServers") |
| 158 | + |
| 159 | + found := false |
| 160 | + for _, authServer := range authorizationServerList { |
| 161 | + if authServer.Id == authorizationServer.Id { |
| 162 | + assert_authorization_server_model(t, authServer) |
| 163 | + found = true |
| 164 | + } |
| 165 | + } |
| 166 | + assert.True(t, found, "Could not find authorization from list") |
| 167 | + |
| 168 | + _, err = client.AuthorizationServer.DeleteAuthorizationServer(authorizationServer.Id) |
| 169 | +} |
| 170 | + |
| 171 | +func Test_can_activate_an_authorizaiton_server(t *testing.T) { |
| 172 | + client, _ := tests.NewClient() |
| 173 | + |
| 174 | + as := okta.AuthorizationServer{ |
| 175 | + Name: "Sample Authorizaiton Server - Golang", |
| 176 | + Description: "Sample Authorizaiton Server Description for Golang", |
| 177 | + Audiences: []string{"api://default"}, |
| 178 | + } |
| 179 | + |
| 180 | + authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(as) |
| 181 | + require.NoError(t, err, "creating an authorizaiton server should not error") |
| 182 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers") |
| 183 | + assert.Equal(t, "ACTIVE", authorizationServer.Status, "should have active status after creating") |
| 184 | + |
| 185 | + response, err = client.AuthorizationServer.DeactivateAuthorizationServer(authorizationServer.Id) |
| 186 | + require.NoError(t, err, "deactivating an authorizaiton server should not error") |
| 187 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers/"+authorizationServer.Id+"/lifecycle/deactivate") |
| 188 | + |
| 189 | + authorizationServer, _, _ = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id) |
| 190 | + assert.Equal(t, "INACTIVE", authorizationServer.Status, "should have inactive status after deactivating") |
| 191 | + |
| 192 | + response, err = client.AuthorizationServer.ActivateAuthorizationServer(authorizationServer.Id) |
| 193 | + require.NoError(t, err, "activating an authorizaiton server should not error") |
| 194 | + tests.Assert_response(t, response, "POST", "/api/v1/authorizationServers/"+authorizationServer.Id+"/lifecycle/activate") |
| 195 | + |
| 196 | + // authorizationServer, response, _ = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id) |
| 197 | + // assert.Equal(t, "ACTIVE", authorizationServer.Status, "should have active status after activating") |
| 198 | + |
| 199 | + _, err = client.AuthorizationServer.DeleteAuthorizationServer(authorizationServer.Id) |
| 200 | +} |
| 201 | + |
| 202 | +func assert_authorization_server_model(t *testing.T, authorizationServer *okta.AuthorizationServer) { |
| 203 | + require.IsType(t, &okta.AuthorizationServer{}, authorizationServer, "did not return `*okta.AuthorizationServer` type as first variable") |
| 204 | + assert.NotEmpty(t, authorizationServer.Id, "id should not be empty") |
| 205 | + assert.NotEmpty(t, authorizationServer.Audiences, "audiences should not be empty") |
| 206 | + assert.NotEmpty(t, authorizationServer.Created, "created should not be empty") |
| 207 | + assert.IsType(t, &time.Time{}, authorizationServer.Created, "created should not be of type `*time.Time`") |
| 208 | + assert.NotEmpty(t, authorizationServer.Credentials, "credentials should not be empty") |
| 209 | + assert.IsType(t, &okta.AuthorizationServerCredentials{}, authorizationServer.Credentials, "credentials should not be of type `*okta.AuthorizationServerCredentials`") |
| 210 | + assert.NotEmpty(t, authorizationServer.Description, "description should not be empty") |
| 211 | + assert.NotEmpty(t, authorizationServer.Issuer, "issuer should not be empty") |
| 212 | + assert.NotEmpty(t, authorizationServer.IssuerMode, "issuerMode should not be empty") |
| 213 | + assert.NotEmpty(t, authorizationServer.LastUpdated, "lastUpdated should not be empty") |
| 214 | + assert.IsType(t, &time.Time{}, authorizationServer.LastUpdated, "lastUpdated should not be of type `*time.Time`") |
| 215 | + assert.NotEmpty(t, authorizationServer.Name, "name should not be empty") |
| 216 | + assert.NotEmpty(t, authorizationServer.Status, "status should not be empty") |
| 217 | +} |
0 commit comments