github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v2/tokens/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/identity/v2/tokens"
     8  	th "github.com/huaweicloud/golangsdk/testhelper"
     9  	"github.com/huaweicloud/golangsdk/testhelper/client"
    10  )
    11  
    12  func tokenPost(t *testing.T, options golangsdk.AuthOptions, requestJSON string) tokens.CreateResult {
    13  	th.SetupHTTP()
    14  	defer th.TeardownHTTP()
    15  	HandleTokenPost(t, requestJSON)
    16  
    17  	return tokens.Create(client.ServiceClient(), options)
    18  }
    19  
    20  func tokenPostErr(t *testing.T, options golangsdk.AuthOptions, expectedErr error) {
    21  	th.SetupHTTP()
    22  	defer th.TeardownHTTP()
    23  	HandleTokenPost(t, "")
    24  
    25  	actualErr := tokens.Create(client.ServiceClient(), options).Err
    26  	th.CheckDeepEquals(t, expectedErr, actualErr)
    27  }
    28  
    29  func TestCreateWithPassword(t *testing.T) {
    30  	options := golangsdk.AuthOptions{
    31  		Username: "me",
    32  		Password: "swordfish",
    33  	}
    34  
    35  	IsSuccessful(t, tokenPost(t, options, `
    36      {
    37        "auth": {
    38          "passwordCredentials": {
    39            "username": "me",
    40            "password": "swordfish"
    41          }
    42        }
    43      }
    44    `))
    45  }
    46  
    47  func TestCreateTokenWithTenantID(t *testing.T) {
    48  	options := golangsdk.AuthOptions{
    49  		Username: "me",
    50  		Password: "opensesame",
    51  		TenantID: "fc394f2ab2df4114bde39905f800dc57",
    52  	}
    53  
    54  	IsSuccessful(t, tokenPost(t, options, `
    55      {
    56        "auth": {
    57          "tenantId": "fc394f2ab2df4114bde39905f800dc57",
    58          "passwordCredentials": {
    59            "username": "me",
    60            "password": "opensesame"
    61          }
    62        }
    63      }
    64    `))
    65  }
    66  
    67  func TestCreateTokenWithTenantName(t *testing.T) {
    68  	options := golangsdk.AuthOptions{
    69  		Username:   "me",
    70  		Password:   "opensesame",
    71  		TenantName: "demo",
    72  	}
    73  
    74  	IsSuccessful(t, tokenPost(t, options, `
    75      {
    76        "auth": {
    77          "tenantName": "demo",
    78          "passwordCredentials": {
    79            "username": "me",
    80            "password": "opensesame"
    81          }
    82        }
    83      }
    84    `))
    85  }
    86  
    87  func TestRequireUsername(t *testing.T) {
    88  	options := golangsdk.AuthOptions{
    89  		Password: "thing",
    90  	}
    91  
    92  	tokenPostErr(t, options, golangsdk.ErrMissingInput{Argument: "Username"})
    93  }
    94  
    95  func tokenGet(t *testing.T, tokenId string) tokens.GetResult {
    96  	th.SetupHTTP()
    97  	defer th.TeardownHTTP()
    98  	HandleTokenGet(t, tokenId)
    99  	return tokens.Get(client.ServiceClient(), tokenId)
   100  }
   101  
   102  func TestGetWithToken(t *testing.T) {
   103  	GetIsSuccessful(t, tokenGet(t, "db22caf43c934e6c829087c41ff8d8d6"))
   104  }