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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk/openstack/identity/v2/tenants"
    10  	"github.com/huaweicloud/golangsdk/openstack/identity/v2/tokens"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  	thclient "github.com/huaweicloud/golangsdk/testhelper/client"
    13  )
    14  
    15  // ExpectedToken is the token that should be parsed from TokenCreationResponse.
    16  var ExpectedToken = &tokens.Token{
    17  	ID:        "aaaabbbbccccdddd",
    18  	ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC),
    19  	Tenant: tenants.Tenant{
    20  		ID:          "fc394f2ab2df4114bde39905f800dc57",
    21  		Name:        "test",
    22  		Description: "There are many tenants. This one is yours.",
    23  		Enabled:     true,
    24  	},
    25  }
    26  
    27  // ExpectedServiceCatalog is the service catalog that should be parsed from TokenCreationResponse.
    28  var ExpectedServiceCatalog = &tokens.ServiceCatalog{
    29  	Entries: []tokens.CatalogEntry{
    30  		{
    31  			Name: "inscrutablewalrus",
    32  			Type: "something",
    33  			Endpoints: []tokens.Endpoint{
    34  				{
    35  					PublicURL: "http://something0:1234/v2/",
    36  					Region:    "region0",
    37  				},
    38  				{
    39  					PublicURL: "http://something1:1234/v2/",
    40  					Region:    "region1",
    41  				},
    42  			},
    43  		},
    44  		{
    45  			Name: "arbitrarypenguin",
    46  			Type: "else",
    47  			Endpoints: []tokens.Endpoint{
    48  				{
    49  					PublicURL: "http://else0:4321/v3/",
    50  					Region:    "region0",
    51  				},
    52  			},
    53  		},
    54  	},
    55  }
    56  
    57  // ExpectedUser is the token that should be parsed from TokenGetResponse.
    58  var ExpectedUser = &tokens.User{
    59  	ID:       "a530fefc3d594c4ba2693a4ecd6be74e",
    60  	Name:     "apiserver",
    61  	Roles:    []tokens.Role{{Name: "member"}, {Name: "service"}},
    62  	UserName: "apiserver",
    63  }
    64  
    65  // TokenCreationResponse is a JSON response that contains ExpectedToken and ExpectedServiceCatalog.
    66  const TokenCreationResponse = `
    67  {
    68  	"access": {
    69  		"token": {
    70  			"issued_at": "2014-01-30T15:30:58.000000Z",
    71  			"expires": "2014-01-31T15:30:58Z",
    72  			"id": "aaaabbbbccccdddd",
    73  			"tenant": {
    74  				"description": "There are many tenants. This one is yours.",
    75  				"enabled": true,
    76  				"id": "fc394f2ab2df4114bde39905f800dc57",
    77  				"name": "test"
    78  			}
    79  		},
    80  		"serviceCatalog": [
    81  			{
    82  				"endpoints": [
    83  					{
    84  						"publicURL": "http://something0:1234/v2/",
    85  						"region": "region0"
    86  					},
    87  					{
    88  						"publicURL": "http://something1:1234/v2/",
    89  						"region": "region1"
    90  					}
    91  				],
    92  				"type": "something",
    93  				"name": "inscrutablewalrus"
    94  			},
    95  			{
    96  				"endpoints": [
    97  					{
    98  						"publicURL": "http://else0:4321/v3/",
    99  						"region": "region0"
   100  					}
   101  				],
   102  				"type": "else",
   103  				"name": "arbitrarypenguin"
   104  			}
   105  		]
   106  	}
   107  }
   108  `
   109  
   110  // TokenGetResponse is a JSON response that contains ExpectedToken and ExpectedUser.
   111  const TokenGetResponse = `
   112  {
   113      "access": {
   114  		"token": {
   115  			"issued_at": "2014-01-30T15:30:58.000000Z",
   116  			"expires": "2014-01-31T15:30:58Z",
   117  			"id": "aaaabbbbccccdddd",
   118  			"tenant": {
   119  				"description": "There are many tenants. This one is yours.",
   120  				"enabled": true,
   121  				"id": "fc394f2ab2df4114bde39905f800dc57",
   122  				"name": "test"
   123  			}
   124  		},
   125          "serviceCatalog": [],
   126  		"user": {
   127              "id": "a530fefc3d594c4ba2693a4ecd6be74e",
   128              "name": "apiserver",
   129              "roles": [
   130                  {
   131                      "name": "member"
   132                  },
   133                  {
   134                      "name": "service"
   135                  }
   136              ],
   137              "roles_links": [],
   138              "username": "apiserver"
   139          }
   140      }
   141  }`
   142  
   143  // HandleTokenPost expects a POST against a /tokens handler, ensures that the request body has been
   144  // constructed properly given certain auth options, and returns the result.
   145  func HandleTokenPost(t *testing.T, requestJSON string) {
   146  	th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
   147  		th.TestMethod(t, r, "POST")
   148  		th.TestHeader(t, r, "Content-Type", "application/json")
   149  		th.TestHeader(t, r, "Accept", "application/json")
   150  		if requestJSON != "" {
   151  			th.TestJSONRequest(t, r, requestJSON)
   152  		}
   153  
   154  		w.WriteHeader(http.StatusOK)
   155  		fmt.Fprintf(w, TokenCreationResponse)
   156  	})
   157  }
   158  
   159  // HandleTokenGet expects a Get against a /tokens handler, ensures that the request body has been
   160  // constructed properly given certain auth options, and returns the result.
   161  func HandleTokenGet(t *testing.T, token string) {
   162  	th.Mux.HandleFunc("/tokens/"+token, func(w http.ResponseWriter, r *http.Request) {
   163  		th.TestMethod(t, r, "GET")
   164  		th.TestHeader(t, r, "Accept", "application/json")
   165  		th.TestHeader(t, r, "X-Auth-Token", thclient.TokenID)
   166  
   167  		w.WriteHeader(http.StatusOK)
   168  		fmt.Fprintf(w, TokenGetResponse)
   169  	})
   170  }
   171  
   172  // IsSuccessful ensures that a CreateResult was successful and contains the correct token and
   173  // service catalog.
   174  func IsSuccessful(t *testing.T, result tokens.CreateResult) {
   175  	token, err := result.ExtractToken()
   176  	th.AssertNoErr(t, err)
   177  	th.CheckDeepEquals(t, ExpectedToken, token)
   178  
   179  	serviceCatalog, err := result.ExtractServiceCatalog()
   180  	th.AssertNoErr(t, err)
   181  	th.CheckDeepEquals(t, ExpectedServiceCatalog, serviceCatalog)
   182  }
   183  
   184  // GetIsSuccessful ensures that a GetResult was successful and contains the correct token and
   185  // User Info.
   186  func GetIsSuccessful(t *testing.T, result tokens.GetResult) {
   187  	token, err := result.ExtractToken()
   188  	th.AssertNoErr(t, err)
   189  	th.CheckDeepEquals(t, ExpectedToken, token)
   190  
   191  	user, err := result.ExtractUser()
   192  	th.AssertNoErr(t, err)
   193  	th.CheckDeepEquals(t, ExpectedUser, user)
   194  }