...

Source file src/github.com/okta/okta-sdk-golang/v2/tests/integration/authorization_server_test.go

Documentation: github.com/okta/okta-sdk-golang/v2/tests/integration

     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  	"context"
    21  	"net/http"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/okta/okta-sdk-golang/v2/okta"
    26  	"github.com/okta/okta-sdk-golang/v2/tests"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func Test_can_create_an_authorizaiton_server(t *testing.T) {
    32  	ctx, client, err := tests.NewClient(context.TODO())
    33  	require.NoError(t, err)
    34  
    35  	as := okta.AuthorizationServer{
    36  		Name:        testName("Sample Authorizaiton Server - Golang"),
    37  		Description: "Sample Authorizaiton Server Description for Golang",
    38  		Audiences:   []string{"api://default"},
    39  	}
    40  
    41  	authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(ctx, as)
    42  	require.NoError(t, err, "creating an authorizaiton server should not error")
    43  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers")
    44  
    45  	assert_authorization_server_model(t, authorizationServer)
    46  
    47  	_, err = client.AuthorizationServer.DeleteAuthorizationServer(ctx, authorizationServer.Id)
    48  	require.NoError(t, err)
    49  }
    50  
    51  func Test_can_get_an_authorizaiton_server(t *testing.T) {
    52  	ctx, client, err := tests.NewClient(context.TODO())
    53  	require.NoError(t, err)
    54  
    55  	as := okta.AuthorizationServer{
    56  		Name:        testName("Sample Authorizaiton Server - Golang"),
    57  		Description: "Sample Authorizaiton Server Description for Golang",
    58  		Audiences:   []string{"api://default"},
    59  	}
    60  
    61  	// NOTE: Org needs FF API_ACCESS_MANAGEMENT else 401s will occur
    62  	authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(ctx, as)
    63  	require.NoError(t, err, "creating an authorizaiton server should not error")
    64  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers")
    65  
    66  	authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(ctx, authorizationServer.Id)
    67  	require.NoError(t, err, "getting an authorizaiton server should not error")
    68  	tests.AssertResponse(t, response, "GET", "/api/v1/authorizationServers/"+authorizationServer.Id)
    69  
    70  	assert_authorization_server_model(t, authorizationServer)
    71  
    72  	assert.Equal(t, as.Name, authorizationServer.Name, "did not return the same authorization server name")
    73  	assert.Equal(t, as.Description, authorizationServer.Description, "did not return the same authorization server description")
    74  
    75  	_, err = client.AuthorizationServer.DeleteAuthorizationServer(ctx, authorizationServer.Id)
    76  	require.NoError(t, err)
    77  }
    78  
    79  func Test_can_update_an_authorizaiton_server(t *testing.T) {
    80  	ctx, client, err := tests.NewClient(context.TODO())
    81  	require.NoError(t, err)
    82  
    83  	as := okta.AuthorizationServer{
    84  		Name:        testName("Sample Authorizaiton Server - Golang"),
    85  		Description: "Sample Authorizaiton Server Description for Golang",
    86  		Audiences:   []string{"api://default"},
    87  	}
    88  
    89  	authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(ctx, as)
    90  	require.NoError(t, err, "creating an authorizaiton server should not error")
    91  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers")
    92  
    93  	authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(ctx, authorizationServer.Id)
    94  	require.NoError(t, err, "getting an authorizaiton server should not error")
    95  	tests.AssertResponse(t, response, "GET", "/api/v1/authorizationServers/"+authorizationServer.Id)
    96  
    97  	assert_authorization_server_model(t, authorizationServer)
    98  
    99  	assert.Equal(t, as.Name, authorizationServer.Name, "did not return the same authorization server name")
   100  	assert.Equal(t, as.Description, authorizationServer.Description, "did not return the same authorization server description")
   101  
   102  	updatedName := "Updated Authorization Server - Golang"
   103  	updatedDescription := "Updated Authorization Server Description"
   104  	authorizationServer.Name = updatedName
   105  	authorizationServer.Description = updatedDescription
   106  
   107  	authorizationServer, response, err = client.AuthorizationServer.UpdateAuthorizationServer(ctx, authorizationServer.Id, *authorizationServer)
   108  	require.NoError(t, err, "getting an authorizaiton server should not error")
   109  	tests.AssertResponse(t, response, "PUT", "/api/v1/authorizationServers/"+authorizationServer.Id)
   110  
   111  	assert_authorization_server_model(t, authorizationServer)
   112  
   113  	assert.Equal(t, updatedName, authorizationServer.Name, "did not return the same authorization server name")
   114  	assert.Equal(t, updatedDescription, authorizationServer.Description, "did not return the same authorization server description")
   115  
   116  	_, err = client.AuthorizationServer.DeleteAuthorizationServer(ctx, authorizationServer.Id)
   117  	require.NoError(t, err)
   118  }
   119  
   120  func Test_can_delete_an_authorizaiton_server(t *testing.T) {
   121  	ctx, client, err := tests.NewClient(context.TODO())
   122  	require.NoError(t, err)
   123  
   124  	as := okta.AuthorizationServer{
   125  		Name:        testName("Sample Authorizaiton Server - Golang"),
   126  		Description: "Sample Authorizaiton Server Description for Golang",
   127  		Audiences:   []string{"api://default"},
   128  	}
   129  
   130  	authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(ctx, as)
   131  	require.NoError(t, err, "creating an authorizaiton server should not error")
   132  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers")
   133  
   134  	authorizationServer, response, err = client.AuthorizationServer.GetAuthorizationServer(ctx, authorizationServer.Id)
   135  	require.NoError(t, err, "getting an authorizaiton server should not error")
   136  	tests.AssertResponse(t, response, "GET", "/api/v1/authorizationServers/"+authorizationServer.Id)
   137  
   138  	assert_authorization_server_model(t, authorizationServer)
   139  
   140  	assert.Equal(t, as.Name, authorizationServer.Name, "did not return the same authorization server name")
   141  	assert.Equal(t, as.Description, authorizationServer.Description, "did not return the same authorization server description")
   142  
   143  	response, err = client.AuthorizationServer.DeleteAuthorizationServer(ctx, authorizationServer.Id)
   144  	require.NoError(t, err)
   145  	assert.Equal(t, http.StatusNoContent, response.StatusCode, "did not return a 204 status code during delete")
   146  
   147  	_, response, err = client.AuthorizationServer.GetAuthorizationServer(ctx, authorizationServer.Id)
   148  	assert.Error(t, err, "Finding an authorization server by id should have reported an error")
   149  	assert.Equal(t, http.StatusNotFound, response.StatusCode, "Should have resulted in a 404 when finding a deleted authorization server")
   150  }
   151  
   152  func Test_can_list_authorizaiton_servers(t *testing.T) {
   153  	ctx, client, err := tests.NewClient(context.TODO())
   154  	require.NoError(t, err)
   155  
   156  	as := okta.AuthorizationServer{
   157  		Name:        testName("Sample Authorizaiton Server - Golang"),
   158  		Description: "Sample Authorizaiton Server Description for Golang",
   159  		Audiences:   []string{"api://default"},
   160  	}
   161  
   162  	authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(ctx, as)
   163  	require.NoError(t, err, "creating an authorizaiton server should not error")
   164  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers")
   165  
   166  	authorizationServerList, response, err := client.AuthorizationServer.ListAuthorizationServers(ctx, nil)
   167  	require.NoError(t, err, "list authorizaiton servers should not error")
   168  	tests.AssertResponse(t, response, "GET", "/api/v1/authorizationServers")
   169  
   170  	found := false
   171  	for _, authServer := range authorizationServerList {
   172  		if authServer.Id == authorizationServer.Id {
   173  			assert_authorization_server_model(t, authServer)
   174  			found = true
   175  		}
   176  	}
   177  	assert.True(t, found, "Could not find authorization from list")
   178  
   179  	_, err = client.AuthorizationServer.DeleteAuthorizationServer(ctx, authorizationServer.Id)
   180  	require.NoError(t, err)
   181  }
   182  
   183  func Test_can_activate_an_authorizaiton_server(t *testing.T) {
   184  	ctx, client, err := tests.NewClient(context.TODO())
   185  	require.NoError(t, err)
   186  
   187  	as := okta.AuthorizationServer{
   188  		Name:        testName("Sample Authorizaiton Server - Golang"),
   189  		Description: "Sample Authorizaiton Server Description for Golang",
   190  		Audiences:   []string{"api://default"},
   191  	}
   192  
   193  	authorizationServer, response, err := client.AuthorizationServer.CreateAuthorizationServer(ctx, as)
   194  	require.NoError(t, err, "creating an authorizaiton server should not error")
   195  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers")
   196  	assert.Equal(t, "ACTIVE", authorizationServer.Status, "should have active status after creating")
   197  
   198  	response, err = client.AuthorizationServer.DeactivateAuthorizationServer(ctx, authorizationServer.Id)
   199  	require.NoError(t, err, "deactivating an authorizaiton server should not error")
   200  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers/"+authorizationServer.Id+"/lifecycle/deactivate")
   201  
   202  	authorizationServer, _, _ = client.AuthorizationServer.GetAuthorizationServer(ctx, authorizationServer.Id)
   203  	assert.Equal(t, "INACTIVE", authorizationServer.Status, "should have inactive status after deactivating")
   204  
   205  	response, err = client.AuthorizationServer.ActivateAuthorizationServer(ctx, authorizationServer.Id)
   206  	require.NoError(t, err, "activating an authorizaiton server should not error")
   207  	tests.AssertResponse(t, response, "POST", "/api/v1/authorizationServers/"+authorizationServer.Id+"/lifecycle/activate")
   208  
   209  	// authorizationServer, response, _ = client.AuthorizationServer.GetAuthorizationServer(authorizationServer.Id)
   210  	// assert.Equal(t, "ACTIVE", authorizationServer.Status, "should have active status after activating")
   211  
   212  	_, err = client.AuthorizationServer.DeleteAuthorizationServer(ctx, authorizationServer.Id)
   213  	require.NoError(t, err)
   214  }
   215  
   216  func assert_authorization_server_model(t *testing.T, authorizationServer *okta.AuthorizationServer) {
   217  	require.IsType(t, &okta.AuthorizationServer{}, authorizationServer, "did not return `*okta.AuthorizationServer` type as first variable")
   218  	assert.NotEmpty(t, authorizationServer.Id, "id should not be empty")
   219  	assert.NotEmpty(t, authorizationServer.Audiences, "audiences should not be empty")
   220  	assert.NotEmpty(t, authorizationServer.Created, "created should not be empty")
   221  	assert.IsType(t, &time.Time{}, authorizationServer.Created, "created should not be of type `*time.Time`")
   222  	assert.NotEmpty(t, authorizationServer.Credentials, "credentials should not be empty")
   223  	assert.IsType(t, &okta.AuthorizationServerCredentials{}, authorizationServer.Credentials, "credentials should not be of type `*okta.AuthorizationServerCredentials`")
   224  	assert.NotEmpty(t, authorizationServer.Description, "description should not be empty")
   225  	assert.NotEmpty(t, authorizationServer.Issuer, "issuer should not be empty")
   226  	assert.NotEmpty(t, authorizationServer.IssuerMode, "issuerMode should not be empty")
   227  	assert.NotEmpty(t, authorizationServer.LastUpdated, "lastUpdated should not be empty")
   228  	assert.IsType(t, &time.Time{}, authorizationServer.LastUpdated, "lastUpdated should not be of type `*time.Time`")
   229  	assert.NotEmpty(t, authorizationServer.Name, "name should not be empty")
   230  	assert.NotEmpty(t, authorizationServer.Status, "status should not be empty")
   231  }
   232  

View as plain text