...

Source file src/github.com/okta/okta-sdk-golang/v2/tests/integration/factor_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  	"testing"
    22  
    23  	"github.com/okta/okta-sdk-golang/v2/okta"
    24  	"github.com/okta/okta-sdk-golang/v2/okta/query"
    25  	"github.com/okta/okta-sdk-golang/v2/tests"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func Test_exercise_factor_lifecycle(t *testing.T) {
    31  	ctx, client, err := tests.NewClient(context.TODO())
    32  	require.NoError(t, err)
    33  
    34  	p := &okta.PasswordCredential{
    35  		Value: "Abcd1234",
    36  	}
    37  	uc := &okta.UserCredentials{
    38  		Password: p,
    39  	}
    40  	profile := okta.UserProfile{}
    41  	profile["firstName"] = "John"
    42  	profile["lastName"] = "Factor-Lifecycle"
    43  	profile["email"] = randomEmail()
    44  	profile["login"] = profile["email"]
    45  	u := &okta.CreateUserRequest{
    46  		Credentials: uc,
    47  		Profile:     &profile,
    48  	}
    49  	qp := query.NewQueryParams(query.WithActivate(false))
    50  
    51  	user, _, err := client.User.CreateUser(ctx, *u, qp)
    52  	require.NoError(t, err, "Creating a new user should not error")
    53  
    54  	allowedFactors, _, _ := client.UserFactor.ListSupportedFactors(ctx, user.Id)
    55  	continueTesting := false
    56  	for _, f := range allowedFactors {
    57  		if f.(*okta.UserFactor).FactorType == "sms" {
    58  			continueTesting = true
    59  		}
    60  	}
    61  
    62  	if continueTesting {
    63  		factors, _, listFactorsError := client.UserFactor.ListFactors(ctx, user.Id)
    64  		require.NoError(t, listFactorsError, "Should not error when listing factors")
    65  
    66  		assert.Empty(t, factors, "Factors list should be empty")
    67  
    68  		factorProfile := okta.NewSmsUserFactorProfile()
    69  		factorProfile.PhoneNumber = "16284001133"
    70  
    71  		factor := okta.NewSmsUserFactor()
    72  		factor.Profile = factorProfile
    73  
    74  		addedFactor, resp, err := client.UserFactor.EnrollFactor(ctx, user.Id, factor, nil)
    75  		require.NotEmpty(t, resp, "Response should not be empty")
    76  		require.NoError(t, err, "Adding factor should not error")
    77  		assert.IsType(t, okta.NewSmsUserFactor(), addedFactor)
    78  
    79  		foundFactor, _, err := client.UserFactor.GetFactor(ctx, user.Id, addedFactor.(*okta.SmsUserFactor).Id, okta.NewSmsUserFactor())
    80  		require.NoError(t, err, "Getting the factor should not error")
    81  
    82  		client.UserFactor.DeleteFactor(ctx, user.Id, foundFactor.(*okta.SmsUserFactor).Id)
    83  	} else {
    84  		t.Skip("Skipping exercise factor lifecycle testing. SMS factor was not enabled")
    85  	}
    86  
    87  	client.User.DeactivateUser(ctx, user.Id, nil)
    88  	client.User.DeactivateOrDeleteUser(ctx, user.Id, nil)
    89  }
    90  

View as plain text