...

Source file src/github.com/okta/okta-sdk-golang/v2/tests/integration/request_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  	"io"
    22  	"testing"
    23  
    24  	"github.com/okta/okta-sdk-golang/v2/okta"
    25  	"github.com/okta/okta-sdk-golang/v2/okta/query"
    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_private_key_request_contains_bearer_token(t *testing.T) {
    32  	var buff io.ReadWriter
    33  
    34  	_, client, err := tests.NewClient(context.TODO(), okta.WithAuthorizationMode("PrivateKey"), okta.WithScopes(([]string{"okta.users.manage"})))
    35  	require.NoError(t, err)
    36  
    37  	request, err := client.CloneRequestExecutor().NewRequest("GET", "https://example.com/", buff)
    38  	require.NoError(t, err)
    39  
    40  	assert.Contains(t, request.Header.Get("Authorization"), "Bearer", "does not contain a bearer token for the request")
    41  }
    42  
    43  func Test_jwt_request_contains_bearer_token(t *testing.T) {
    44  	var buff io.ReadWriter
    45  
    46  	_, client, err := tests.NewClient(context.TODO())
    47  	require.NoError(t, err)
    48  
    49  	privateKeySigner, err := okta.CreateKeySigner(client.GetConfig().Okta.Client.PrivateKey, client.GetConfig().Okta.Client.PrivateKeyId)
    50  	require.NoError(t, err)
    51  
    52  	clientAssertion, err := okta.CreateClientAssertion(client.GetConfig().Okta.Client.OrgUrl, client.GetConfig().Okta.Client.ClientId, privateKeySigner)
    53  	require.NoError(t, err)
    54  
    55  	err = client.SetConfig(okta.WithAuthorizationMode("JWT"), okta.WithScopes(([]string{"okta.users.manage"})), okta.WithClientAssertion(clientAssertion))
    56  	require.NoError(t, err)
    57  
    58  	request, err := client.CloneRequestExecutor().NewRequest("GET", "https://example.com/", buff)
    59  	require.NoError(t, err)
    60  
    61  	assert.Contains(t, request.Header.Get("Authorization"), "Bearer", "does not contain a bearer token for the request")
    62  }
    63  
    64  func Test_private_key_request_can_create_a_user(t *testing.T) {
    65  	ctx, client, err := tests.NewClient(context.TODO(), okta.WithAuthorizationMode("PrivateKey"), okta.WithScopes(([]string{"okta.users.manage"})))
    66  	require.NoError(t, err)
    67  
    68  	p := &okta.PasswordCredential{
    69  		Value: "Abcd1234",
    70  	}
    71  	uc := &okta.UserCredentials{
    72  		Password: p,
    73  	}
    74  	email := randomEmail()
    75  	profile := okta.UserProfile{}
    76  	profile["firstName"] = "John"
    77  	profile["lastName"] = "Private_Key"
    78  	profile["email"] = email
    79  	profile["login"] = email
    80  	u := &okta.CreateUserRequest{
    81  		Credentials: uc,
    82  		Profile:     &profile,
    83  	}
    84  
    85  	qp := query.NewQueryParams(query.WithActivate(false))
    86  
    87  	user, _, err := client.User.CreateUser(ctx, *u, qp)
    88  	require.NoError(t, err, "Creating a new user should not error")
    89  	assert.NotEmpty(t, user.Id, "appears the user was not created")
    90  	tempProfile := *user.Profile
    91  	assert.Equal(t, email, tempProfile["email"], "did not get the correct user")
    92  
    93  	// Deactivate the user → POST /api/v1/users/{{userId}}/lifecycle/deactivate
    94  	_, err = client.User.DeactivateUser(ctx, user.Id, nil)
    95  	require.NoError(t, err, "Should not error when deactivating")
    96  
    97  	// Delete the user → DELETE /api/v1/users/{{userId}}
    98  	_, err = client.User.DeactivateOrDeleteUser(ctx, user.Id, nil)
    99  	require.NoError(t, err, "Should not error when deleting")
   100  }
   101  

View as plain text