github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/testing/auth_options_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
     8  )
     9  
    10  func TestToTokenV3ScopeMap(t *testing.T) {
    11  	projectID := "685038cd-3c25-4faf-8f9b-78c18e503190"
    12  	projectName := "admin"
    13  	domainID := "e4b515b8-e453-49d8-9cce-4bec244fa84e"
    14  	domainName := "Default"
    15  
    16  	var successCases = []struct {
    17  		name     string
    18  		opts     gophercloud.AuthOptions
    19  		expected map[string]any
    20  	}{
    21  		{
    22  			"System-scoped",
    23  			gophercloud.AuthOptions{
    24  				Scope: &gophercloud.AuthScope{
    25  					System: true,
    26  				},
    27  			},
    28  			map[string]any{
    29  				"system": map[string]any{
    30  					"all": true,
    31  				},
    32  			},
    33  		},
    34  		{
    35  			"Trust-scoped",
    36  			gophercloud.AuthOptions{
    37  				Scope: &gophercloud.AuthScope{
    38  					TrustID: "05144328-1f7d-46a9-a978-17eaad187077",
    39  				},
    40  			},
    41  			map[string]any{
    42  				"OS-TRUST:trust": map[string]string{
    43  					"id": "05144328-1f7d-46a9-a978-17eaad187077",
    44  				},
    45  			},
    46  		},
    47  		{
    48  			"Project-scoped (ID)",
    49  			gophercloud.AuthOptions{
    50  				Scope: &gophercloud.AuthScope{
    51  					ProjectID: projectID,
    52  				},
    53  			},
    54  			map[string]any{
    55  				"project": map[string]any{
    56  					"id": &projectID,
    57  				},
    58  			},
    59  		},
    60  		{
    61  			"Project-scoped (name)",
    62  			gophercloud.AuthOptions{
    63  				Scope: &gophercloud.AuthScope{
    64  					ProjectName: projectName,
    65  					DomainName:  domainName,
    66  				},
    67  			},
    68  			map[string]any{
    69  				"project": map[string]any{
    70  					"name": &projectName,
    71  					"domain": map[string]any{
    72  						"name": &domainName,
    73  					},
    74  				},
    75  			},
    76  		},
    77  		{
    78  			"Domain-scoped (ID)",
    79  			gophercloud.AuthOptions{
    80  				Scope: &gophercloud.AuthScope{
    81  					DomainID: domainID,
    82  				},
    83  			},
    84  			map[string]any{
    85  				"domain": map[string]any{
    86  					"id": &domainID,
    87  				},
    88  			},
    89  		},
    90  		{
    91  			"Domain-scoped (name)",
    92  			gophercloud.AuthOptions{
    93  				Scope: &gophercloud.AuthScope{
    94  					DomainName: domainName,
    95  				},
    96  			},
    97  			map[string]any{
    98  				"domain": map[string]any{
    99  					"name": &domainName,
   100  				},
   101  			},
   102  		},
   103  		{
   104  			"Empty with project fallback (ID)",
   105  			gophercloud.AuthOptions{
   106  				TenantID: projectID,
   107  				Scope:    nil,
   108  			},
   109  			map[string]any{
   110  				"project": map[string]any{
   111  					"id": &projectID,
   112  				},
   113  			},
   114  		},
   115  		{
   116  			"Empty with project fallback (name)",
   117  			gophercloud.AuthOptions{
   118  				TenantName: projectName,
   119  				DomainName: domainName,
   120  				Scope:      nil,
   121  			},
   122  			map[string]any{
   123  				"project": map[string]any{
   124  					"name": &projectName,
   125  					"domain": map[string]any{
   126  						"name": &domainName,
   127  					},
   128  				},
   129  			},
   130  		},
   131  		{
   132  			"Empty without fallback",
   133  			gophercloud.AuthOptions{
   134  				Scope: nil,
   135  			},
   136  			nil,
   137  		},
   138  	}
   139  	for _, successCase := range successCases {
   140  		t.Run(successCase.name, func(t *testing.T) {
   141  			actual, err := successCase.opts.ToTokenV3ScopeMap()
   142  			th.AssertNoErr(t, err)
   143  			th.AssertDeepEquals(t, successCase.expected, actual)
   144  		})
   145  	}
   146  
   147  	var failCases = []struct {
   148  		name     string
   149  		opts     gophercloud.AuthOptions
   150  		expected error
   151  	}{
   152  		{
   153  			"Project-scoped with name but missing domain ID/name",
   154  			gophercloud.AuthOptions{
   155  				Scope: &gophercloud.AuthScope{
   156  					ProjectName: "admin",
   157  				},
   158  			},
   159  			gophercloud.ErrScopeDomainIDOrDomainName{},
   160  		},
   161  		{
   162  			"Project-scoped with both project name and project ID",
   163  			gophercloud.AuthOptions{
   164  				Scope: &gophercloud.AuthScope{
   165  					ProjectName: "admin",
   166  					ProjectID:   "685038cd-3c25-4faf-8f9b-78c18e503190",
   167  					DomainName:  "Default",
   168  				},
   169  			},
   170  			gophercloud.ErrScopeProjectIDOrProjectName{},
   171  		},
   172  		{
   173  			"Project-scoped with name and unnecessary domain ID",
   174  			gophercloud.AuthOptions{
   175  				Scope: &gophercloud.AuthScope{
   176  					ProjectID: "685038cd-3c25-4faf-8f9b-78c18e503190",
   177  					DomainID:  "e4b515b8-e453-49d8-9cce-4bec244fa84e",
   178  				},
   179  			},
   180  			gophercloud.ErrScopeProjectIDAlone{},
   181  		},
   182  		{
   183  			"Project-scoped with name and unnecessary domain name",
   184  			gophercloud.AuthOptions{
   185  				Scope: &gophercloud.AuthScope{
   186  					ProjectID:  "685038cd-3c25-4faf-8f9b-78c18e503190",
   187  					DomainName: "Default",
   188  				},
   189  			},
   190  			gophercloud.ErrScopeProjectIDAlone{},
   191  		},
   192  		{
   193  			"Domain-scoped with both domain name and domain ID",
   194  			gophercloud.AuthOptions{
   195  				Scope: &gophercloud.AuthScope{
   196  					DomainID:   "e4b515b8-e453-49d8-9cce-4bec244fa84e",
   197  					DomainName: "Default",
   198  				},
   199  			},
   200  			gophercloud.ErrScopeDomainIDOrDomainName{},
   201  		},
   202  	}
   203  	for _, failCase := range failCases {
   204  		t.Run(failCase.name, func(t *testing.T) {
   205  			_, err := failCase.opts.ToTokenV3ScopeMap()
   206  			th.AssertTypeEquals(t, failCase.expected, err)
   207  		})
   208  	}
   209  }