github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/apigw/dedicated/v2/apis/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/chnsz/golangsdk"
     9  	"github.com/chnsz/golangsdk/openstack/apigw/dedicated/v2/apis"
    10  	th "github.com/chnsz/golangsdk/testhelper"
    11  	"github.com/chnsz/golangsdk/testhelper/client"
    12  )
    13  
    14  const (
    15  	expectedCreateResponse = `
    16  {
    17  	"arrange_necessary": 2,
    18  	"auth_opt": {
    19  		"app_code_auth_type": "DISABLE"
    20  	},
    21  	"auth_type": "AUTHORIZER",
    22  	"authorizer_id": "8b9c7d67ca144a5da7bc9cbccedfe753",
    23  	"backend_api": {
    24  		"enable_client_ssl": false,
    25  		"req_method": "GET",
    26  		"req_protocol": "HTTP",
    27  		"req_uri": "/backend/users",
    28  		"timeout": 6000,
    29  		"url_domain": "69bb5628fce741a1b901b08cde7b814d",
    30  		"vpc_channel_info": {
    31  			"vpc_channel_id": "69bb5628fce741a1b901b08cde7b814d"
    32  		},
    33  		"vpc_channel_status": 1
    34  	},
    35  	"backend_params": [
    36  		{
    37  			"location": "HEADER",
    38  			"name": "X-User-Name",
    39  			"origin": "SYSTEM",
    40  			"value": "$context.authorizer.frontend.user_name"
    41  		}
    42  	],
    43  	"backend_type": "HTTP",
    44  	"cors": false,
    45  	"group_id": "c6e46c49a6734c918262a16c3c1a3a13",
    46  	"group_name": "terraform_test",
    47  	"group_version": "V1",
    48  	"id": "cded6d80fc9f442c9842eaf854f10525",
    49  	"match_mode": "NORMAL",
    50  	"name": "terraform_test",
    51  	"register_time": "2021-08-05T03:33:35.360020923Z",
    52  	"req_method": "POST",
    53  	"req_protocol": "HTTP",
    54  	"req_uri": "/terraform/users",
    55  	"status": 1,
    56  	"type": 2,
    57  	"update_time": "2021-08-05T03:33:35.360021226Z"
    58  }`
    59  
    60  	expectedGetResponse = `
    61  {
    62  	"arrange_necessary": 2,
    63  	"auth_opt": {
    64  		"app_code_auth_type": "DISABLE"
    65  	},
    66  	"auth_type": "AUTHORIZER",
    67  	"authorizer_id": "8b9c7d67ca144a5da7bc9cbccedfe753",
    68  	"backend_api": {
    69  		"enable_client_ssl": false,
    70  		"req_method": "GET",
    71  		"req_protocol": "HTTP",
    72  		"req_uri": "/backend/users",
    73  		"timeout": 6000,
    74  		"url_domain": "69bb5628fce741a1b901b08cde7b814d",
    75  		"vpc_channel_info": {
    76  			"vpc_channel_id": "69bb5628fce741a1b901b08cde7b814d"
    77  		},
    78  		"vpc_channel_status": 1
    79  	},
    80  	"backend_params": [
    81  		{
    82  		  "location": "HEADER",
    83  		  "name": "X-User-Name",
    84  		  "origin": "SYSTEM",
    85  		  "value": "$context.authorizer.frontend.user_name"
    86  		}
    87  	],
    88  	"backend_type": "HTTP",
    89  	"cors": false,
    90  	"group_id": "c6e46c49a6734c918262a16c3c1a3a13",
    91  	"group_name": "terraform_test",
    92  	"group_version": "V1",
    93  	"id": "cded6d80fc9f442c9842eaf854f10525",
    94  	"match_mode": "NORMAL",
    95  	"name": "terraform_test",
    96  	"register_time": "2021-08-05T03:33:35.360020923Z",
    97  	"req_method": "POST",
    98  	"req_protocol": "HTTP",
    99  	"req_uri": "/terraform/users",
   100  	"status": 1,
   101  	"type": 2,
   102  	"update_time": "2021-08-05T03:33:35Z"
   103  }
   104  `
   105  
   106  	expectedListResponse = `
   107  {
   108  	"total": 1,
   109  	"size": 1,
   110  	"apis": [
   111  		{
   112  			"arrange_necessary": 2,
   113  			"auth_opt": {
   114  				"app_code_auth_type": "DISABLE"
   115  			},
   116  			"auth_type": "AUTHORIZER",
   117  			"authorizer_id": "8b9c7d67ca144a5da7bc9cbccedfe753",
   118  			"backend_api": {
   119  				"enable_client_ssl": false,
   120  				"req_method": "GET",
   121  				"req_protocol": "HTTP",
   122  				"req_uri": "/backend/users",
   123  				"timeout": 6000,
   124  				"url_domain": "69bb5628fce741a1b901b08cde7b814d",
   125  				"vpc_channel_info": {
   126  					"vpc_channel_id": "69bb5628fce741a1b901b08cde7b814d"
   127  				},
   128  				"vpc_channel_status": 1
   129  			},
   130  			"backend_params": [
   131  				{
   132  				  "location": "HEADER",
   133  				  "name": "X-User-Name",
   134  				  "origin": "SYSTEM",
   135  				  "value": "$context.authorizer.frontend.user_name"
   136  				}
   137  			],
   138  			"backend_type": "HTTP",
   139  			"cors": false,
   140  			"group_id": "c6e46c49a6734c918262a16c3c1a3a13",
   141  			"group_name": "terraform_test",
   142  			"group_version": "V1",
   143  			"id": "cded6d80fc9f442c9842eaf854f10525",
   144  			"match_mode": "NORMAL",
   145  			"name": "terraform_test",
   146  			"register_time": "2021-08-05T03:33:35.360020923Z",
   147  			"req_method": "POST",
   148  			"req_protocol": "HTTP",
   149  			"req_uri": "/terraform/users",
   150  			"status": 1,
   151  			"type": 2,
   152  			"update_time": "2021-08-05T03:33:35Z"
   153  		}
   154  	]
   155  }`
   156  
   157  	expectedPublishAPIResponse = `
   158  {
   159  	"api_id": "2b0253cba7d348f698de45abacd3ae29",
   160  	"env_id": "c5b32727186c4fe6b60408a8a297be09",
   161  	"publish_id": "8ecdb96299a64e10ad1c7f37a5d24bdb",
   162  	"publish_time": "2021-10-12T07:02:12.72743121Z",
   163  	"remark": "Version 1",
   164  	"version_id": "eaf45032b6a649349cfbfe736d41a711"
   165  }`
   166  
   167  	expectedListPublishHistoriesResponse = `
   168  {
   169  	"api_versions": [
   170  		{
   171  			"api_id": "2b0253cba7d348f698de45abacd3ae29",
   172  			"env_id": "c5b32727186c4fe6b60408a8a297be09",
   173  			"env_name": "tf_lance_apig_environment_demo",
   174  			"publish_time": "2021-10-12T07:02:12Z",
   175  			"remark": "Version 1",
   176  			"status": 1,
   177  			"version_id": "eaf45032b6a649349cfbfe736d41a711",
   178  			"version_no": "20211012150212"
   179  		}
   180  	]
   181  }`
   182  
   183  	expectedVersionSwitchResponse = `
   184  {
   185  	"api_id": "2b0253cba7d348f698de45abacd3ae29",
   186  	"env_id": "c5b32727186c4fe6b60408a8a297be09",
   187  	"publish_id": "b8a167517eca451f9f0a68d1e7ee96b8",
   188  	"publish_time": "2021-10-12T07:37:00.212793535Z",
   189  	"remark": "Version 1",
   190  	"version_id": "eaf45032b6a649349cfbfe736d41a711"
   191  }`
   192  
   193  	expectedOfflineAPIResponse = `
   194  {
   195  	"api_id": "2b0253cba7d348f698de45abacd3ae29",
   196  	"env_id": "c5b32727186c4fe6b60408a8a297be09",
   197  	"api_name": "tf_lance_apig_api_demo",
   198  	"publish_time": "0001-01-01T00:00:00Z"
   199  }`
   200  )
   201  
   202  var (
   203  	createOpts = apis.APIOpts{
   204  		GroupId:      "c6e46c49a6734c918262a16c3c1a3a13",
   205  		Type:         2,
   206  		AuthType:     "AUTHORIZER",
   207  		AuthorizerId: "8b9c7d67ca144a5da7bc9cbccedfe753",
   208  		Cors:         golangsdk.Disabled,
   209  		MatchMode:    "NORMAL",
   210  		Name:         "terraform_test",
   211  		ReqURI:       "/terraform/users",
   212  		ReqMethod:    "POST",
   213  		ReqProtocol:  "HTTP",
   214  		BackendType:  "HTTP",
   215  		WebInfo: &apis.Web{
   216  			ClientSslEnable: golangsdk.Disabled,
   217  			ReqMethod:       "POST",
   218  			ReqProtocol:     "HTTP",
   219  			ReqURI:          "/backend/users",
   220  			Timeout:         6000,
   221  			VpcChannelInfo: &apis.VpcChannel{
   222  				VpcChannelId: "69bb5628fce741a1b901b08cde7b814d",
   223  			},
   224  			VpcChannelStatus: 1,
   225  		},
   226  		BackendParams: []apis.BackendParamBase{
   227  			{
   228  				Location: "HEADER",
   229  				Name:     "X-User-Name",
   230  				Origin:   "SYSTEM",
   231  				Value:    "$context.authorizer.frontend.user_name",
   232  			},
   233  		},
   234  	}
   235  
   236  	expectedCreateResponseData = &apis.APIResp{
   237  		ArrangeNecessary: 2,
   238  		AuthType:         "AUTHORIZER",
   239  		AuthorizerId:     "8b9c7d67ca144a5da7bc9cbccedfe753",
   240  		AuthOpt: apis.AuthOpt{
   241  			AppCodeAuthType: "DISABLE",
   242  		},
   243  		WebInfo: apis.Web{
   244  			ClientSslEnable: golangsdk.Disabled,
   245  			ReqMethod:       "GET",
   246  			ReqProtocol:     "HTTP",
   247  			ReqURI:          "/backend/users",
   248  			Timeout:         6000,
   249  			DomainURL:       "69bb5628fce741a1b901b08cde7b814d",
   250  			VpcChannelInfo: &apis.VpcChannel{
   251  				VpcChannelId: "69bb5628fce741a1b901b08cde7b814d",
   252  			},
   253  			VpcChannelStatus: 1,
   254  		},
   255  		BackendParams: []apis.BackendParamResp{
   256  			{
   257  				Location: "HEADER",
   258  				Name:     "X-User-Name",
   259  				Origin:   "SYSTEM",
   260  				Value:    "$context.authorizer.frontend.user_name",
   261  			},
   262  		},
   263  		BackendType:  "HTTP",
   264  		Cors:         false,
   265  		GroupId:      "c6e46c49a6734c918262a16c3c1a3a13",
   266  		GroupName:    "terraform_test",
   267  		GroupVersion: "V1",
   268  		ID:           "cded6d80fc9f442c9842eaf854f10525",
   269  		MatchMode:    "NORMAL",
   270  		Name:         "terraform_test",
   271  		RegisterTime: "2021-08-05T03:33:35.360020923Z",
   272  		ReqMethod:    "POST",
   273  		ReqProtocol:  "HTTP",
   274  		ReqURI:       "/terraform/users",
   275  		Status:       1,
   276  		Type:         2,
   277  		UpdateTime:   "2021-08-05T03:33:35.360021226Z",
   278  	}
   279  
   280  	expectedGetResponseData = &apis.APIResp{
   281  		ArrangeNecessary: 2,
   282  		AuthType:         "AUTHORIZER",
   283  		AuthorizerId:     "8b9c7d67ca144a5da7bc9cbccedfe753",
   284  		AuthOpt: apis.AuthOpt{
   285  			AppCodeAuthType: "DISABLE",
   286  		},
   287  		WebInfo: apis.Web{
   288  			ClientSslEnable: golangsdk.Disabled,
   289  			ReqMethod:       "GET",
   290  			ReqProtocol:     "HTTP",
   291  			ReqURI:          "/backend/users",
   292  			Timeout:         6000,
   293  			DomainURL:       "69bb5628fce741a1b901b08cde7b814d",
   294  			VpcChannelInfo: &apis.VpcChannel{
   295  				VpcChannelId: "69bb5628fce741a1b901b08cde7b814d",
   296  			},
   297  			VpcChannelStatus: 1,
   298  		},
   299  		BackendParams: []apis.BackendParamResp{
   300  			{
   301  				Location: "HEADER",
   302  				Name:     "X-User-Name",
   303  				Origin:   "SYSTEM",
   304  				Value:    "$context.authorizer.frontend.user_name",
   305  			},
   306  		},
   307  		BackendType:  "HTTP",
   308  		Cors:         false,
   309  		GroupId:      "c6e46c49a6734c918262a16c3c1a3a13",
   310  		GroupName:    "terraform_test",
   311  		GroupVersion: "V1",
   312  		ID:           "cded6d80fc9f442c9842eaf854f10525",
   313  		MatchMode:    "NORMAL",
   314  		Name:         "terraform_test",
   315  		RegisterTime: "2021-08-05T03:33:35.360020923Z",
   316  		ReqMethod:    "POST",
   317  		ReqProtocol:  "HTTP",
   318  		ReqURI:       "/terraform/users",
   319  		Status:       1,
   320  		Type:         2,
   321  		UpdateTime:   "2021-08-05T03:33:35Z",
   322  	}
   323  
   324  	listOpts = &apis.ListOpts{
   325  		Name: "terraform_test",
   326  	}
   327  
   328  	expectedListResponseData = []apis.APIResp{
   329  		{
   330  			ArrangeNecessary: 2,
   331  			AuthType:         "AUTHORIZER",
   332  			AuthorizerId:     "8b9c7d67ca144a5da7bc9cbccedfe753",
   333  			AuthOpt: apis.AuthOpt{
   334  				AppCodeAuthType: "DISABLE",
   335  			},
   336  			WebInfo: apis.Web{
   337  				ClientSslEnable: golangsdk.Disabled,
   338  				ReqMethod:       "GET",
   339  				ReqProtocol:     "HTTP",
   340  				ReqURI:          "/backend/users",
   341  				Timeout:         6000,
   342  				DomainURL:       "69bb5628fce741a1b901b08cde7b814d",
   343  				VpcChannelInfo: &apis.VpcChannel{
   344  					VpcChannelId: "69bb5628fce741a1b901b08cde7b814d",
   345  				},
   346  				VpcChannelStatus: 1,
   347  			},
   348  			BackendParams: []apis.BackendParamResp{
   349  				{
   350  					Location: "HEADER",
   351  					Name:     "X-User-Name",
   352  					Origin:   "SYSTEM",
   353  					Value:    "$context.authorizer.frontend.user_name",
   354  				},
   355  			},
   356  			BackendType:  "HTTP",
   357  			Cors:         false,
   358  			GroupId:      "c6e46c49a6734c918262a16c3c1a3a13",
   359  			GroupName:    "terraform_test",
   360  			GroupVersion: "V1",
   361  			ID:           "cded6d80fc9f442c9842eaf854f10525",
   362  			MatchMode:    "NORMAL",
   363  			Name:         "terraform_test",
   364  			RegisterTime: "2021-08-05T03:33:35.360020923Z",
   365  			ReqMethod:    "POST",
   366  			ReqProtocol:  "HTTP",
   367  			ReqURI:       "/terraform/users",
   368  			Status:       1,
   369  			Type:         2,
   370  			UpdateTime:   "2021-08-05T03:33:35Z",
   371  		},
   372  	}
   373  
   374  	publishOpts = apis.PublishOpts{
   375  		Action:      "online",
   376  		ApiId:       "2b0253cba7d348f698de45abacd3ae29",
   377  		EnvId:       "c5b32727186c4fe6b60408a8a297be09",
   378  		Description: "Version 1",
   379  	}
   380  
   381  	expectedPublishResponseData = &apis.PublishResp{
   382  		ApiId:       "2b0253cba7d348f698de45abacd3ae29",
   383  		EnvId:       "c5b32727186c4fe6b60408a8a297be09",
   384  		PublishId:   "8ecdb96299a64e10ad1c7f37a5d24bdb",
   385  		PublishTime: "2021-10-12T07:02:12.72743121Z",
   386  		Description: "Version 1",
   387  		VersionId:   "eaf45032b6a649349cfbfe736d41a711",
   388  	}
   389  
   390  	listPublishHistoriesOpts = apis.ListPublishHistoriesOpts{}
   391  
   392  	expectedListPublishHistoriesResponseData = []apis.ApiVersionInfo{
   393  		{
   394  			ApiId:       "2b0253cba7d348f698de45abacd3ae29",
   395  			EnvId:       "c5b32727186c4fe6b60408a8a297be09",
   396  			EnvName:     "tf_lance_apig_environment_demo",
   397  			PublishTime: "2021-10-12T07:02:12Z",
   398  			Description: "Version 1",
   399  			Status:      1,
   400  			VersionId:   "eaf45032b6a649349cfbfe736d41a711",
   401  			Version:     "20211012150212",
   402  		},
   403  	}
   404  
   405  	expectedVersionSwitchResponseData = &apis.PublishResp{
   406  		ApiId:       "2b0253cba7d348f698de45abacd3ae29",
   407  		EnvId:       "c5b32727186c4fe6b60408a8a297be09",
   408  		PublishId:   "b8a167517eca451f9f0a68d1e7ee96b8",
   409  		PublishTime: "2021-10-12T07:37:00.212793535Z",
   410  		Description: "Version 1",
   411  		VersionId:   "eaf45032b6a649349cfbfe736d41a711",
   412  	}
   413  
   414  	offlineOpts = apis.PublishOpts{
   415  		Action: "offline",
   416  		ApiId:  "2b0253cba7d348f698de45abacd3ae29",
   417  		EnvId:  "c5b32727186c4fe6b60408a8a297be09",
   418  	}
   419  
   420  	expectedOfflineAPIResponseData = &apis.PublishResp{
   421  		ApiId:       "2b0253cba7d348f698de45abacd3ae29",
   422  		EnvId:       "c5b32727186c4fe6b60408a8a297be09",
   423  		ApiName:     "tf_lance_apig_api_demo",
   424  		PublishTime: "0001-01-01T00:00:00Z",
   425  	}
   426  )
   427  
   428  func handleV2APICreate(t *testing.T) {
   429  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis",
   430  		func(w http.ResponseWriter, r *http.Request) {
   431  			th.TestMethod(t, r, "POST")
   432  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   433  			w.Header().Add("Content-Type", "application/json")
   434  			w.WriteHeader(http.StatusCreated)
   435  			_, _ = fmt.Fprint(w, expectedCreateResponse)
   436  		})
   437  }
   438  
   439  func handleV2APIGet(t *testing.T) {
   440  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/cded6d80fc9f442c9842eaf854f10525",
   441  		func(w http.ResponseWriter, r *http.Request) {
   442  			th.TestMethod(t, r, "GET")
   443  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   444  			w.Header().Add("Content-Type", "application/json")
   445  			w.WriteHeader(http.StatusOK)
   446  			_, _ = fmt.Fprint(w, expectedGetResponse)
   447  		})
   448  }
   449  
   450  func handleV2APIList(t *testing.T) {
   451  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis",
   452  		func(w http.ResponseWriter, r *http.Request) {
   453  			th.TestMethod(t, r, "GET")
   454  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   455  			w.Header().Add("Content-Type", "application/json")
   456  			w.WriteHeader(http.StatusOK)
   457  			_, _ = fmt.Fprint(w, expectedListResponse)
   458  		})
   459  }
   460  
   461  func handleV2APIUpdate(t *testing.T) {
   462  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/cded6d80fc9f442c9842eaf854f10525",
   463  		func(w http.ResponseWriter, r *http.Request) {
   464  			th.TestMethod(t, r, "PUT")
   465  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   466  			w.Header().Add("Content-Type", "application/json")
   467  			w.WriteHeader(http.StatusOK)
   468  			_, _ = fmt.Fprint(w, expectedGetResponse)
   469  		})
   470  }
   471  
   472  func handleV2APIDelete(t *testing.T) {
   473  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/cded6d80fc9f442c9842eaf854f10525",
   474  		func(w http.ResponseWriter, r *http.Request) {
   475  			th.TestMethod(t, r, "DELETE")
   476  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   477  			w.Header().Add("Content-Type", "application/json")
   478  			w.WriteHeader(http.StatusNoContent)
   479  		})
   480  }
   481  
   482  func handleV2APIPublish(t *testing.T) {
   483  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/action",
   484  		func(w http.ResponseWriter, r *http.Request) {
   485  			th.TestMethod(t, r, "POST")
   486  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   487  			w.Header().Add("Content-Type", "application/json")
   488  			w.WriteHeader(http.StatusCreated)
   489  			_, _ = fmt.Fprint(w, expectedPublishAPIResponse)
   490  		})
   491  }
   492  
   493  func handleV2APIVersionSwitch(t *testing.T) {
   494  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/publish/2b0253cba7d348f698de45abacd3ae29",
   495  		func(w http.ResponseWriter, r *http.Request) {
   496  			th.TestMethod(t, r, "PUT")
   497  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   498  			w.Header().Add("Content-Type", "application/json")
   499  			w.WriteHeader(http.StatusOK)
   500  			_, _ = fmt.Fprint(w, expectedVersionSwitchResponse)
   501  		})
   502  }
   503  
   504  func handleV2APIListPublishHistories(t *testing.T) {
   505  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/publish/2b0253cba7d348f698de45abacd3ae29",
   506  		func(w http.ResponseWriter, r *http.Request) {
   507  			th.TestMethod(t, r, "GET")
   508  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   509  			w.Header().Add("Content-Type", "application/json")
   510  			w.WriteHeader(http.StatusOK)
   511  			_, _ = fmt.Fprint(w, expectedListPublishHistoriesResponse)
   512  		})
   513  }
   514  
   515  func handleV2APIOffline(t *testing.T) {
   516  	th.Mux.HandleFunc("/instances/33fc92ffb7e749df952ecc7729d972bc/apis/action",
   517  		func(w http.ResponseWriter, r *http.Request) {
   518  			th.TestMethod(t, r, "POST")
   519  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   520  			w.Header().Add("Content-Type", "application/json")
   521  			w.WriteHeader(http.StatusCreated)
   522  			_, _ = fmt.Fprint(w, expectedOfflineAPIResponse)
   523  		})
   524  }