github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/vpnaas/ipsecpolicies/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/vpnaas/ipsecpolicies"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    12  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    13  )
    14  
    15  func TestCreate(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/vpn/ipsecpolicies", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "POST")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  		th.TestHeader(t, r, "Content-Type", "application/json")
    23  		th.TestHeader(t, r, "Accept", "application/json")
    24  		th.TestJSONRequest(t, r, `
    25  {
    26      "ipsecpolicy": {
    27          "name": "ipsecpolicy1",
    28          "transform_protocol": "esp",
    29          "auth_algorithm": "sha1",
    30          "encapsulation_mode": "tunnel",
    31          "encryption_algorithm": "aes-128",
    32          "pfs": "group5",
    33          "lifetime": {
    34              "units": "seconds",
    35              "value": 7200
    36          },
    37          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b"
    38  }
    39  }      `)
    40  
    41  		w.Header().Add("Content-Type", "application/json")
    42  		w.WriteHeader(http.StatusCreated)
    43  
    44  		fmt.Fprint(w, `
    45  {
    46      "ipsecpolicy": {
    47          "name": "ipsecpolicy1",
    48          "transform_protocol": "esp",
    49          "auth_algorithm": "sha1",
    50          "encapsulation_mode": "tunnel",
    51          "encryption_algorithm": "aes-128",
    52          "pfs": "group5",
    53          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    54          "project_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    55          "lifetime": {
    56              "units": "seconds",
    57              "value": 7200
    58          },
    59          "id": "5291b189-fd84-46e5-84bd-78f40c05d69c",
    60          "description": ""
    61      }
    62  }
    63      `)
    64  	})
    65  
    66  	lifetime := ipsecpolicies.LifetimeCreateOpts{
    67  		Units: ipsecpolicies.UnitSeconds,
    68  		Value: 7200,
    69  	}
    70  	options := ipsecpolicies.CreateOpts{
    71  		TenantID:            "b4eedccc6fb74fa8a7ad6b08382b852b",
    72  		Name:                "ipsecpolicy1",
    73  		TransformProtocol:   ipsecpolicies.TransformProtocolESP,
    74  		AuthAlgorithm:       ipsecpolicies.AuthAlgorithmSHA1,
    75  		EncapsulationMode:   ipsecpolicies.EncapsulationModeTunnel,
    76  		EncryptionAlgorithm: ipsecpolicies.EncryptionAlgorithmAES128,
    77  		PFS:                 ipsecpolicies.PFSGroup5,
    78  		Lifetime:            &lifetime,
    79  		Description:         "",
    80  	}
    81  	actual, err := ipsecpolicies.Create(context.TODO(), fake.ServiceClient(), options).Extract()
    82  	th.AssertNoErr(t, err)
    83  	expectedLifetime := ipsecpolicies.Lifetime{
    84  		Units: "seconds",
    85  		Value: 7200,
    86  	}
    87  	expected := ipsecpolicies.Policy{
    88  		TenantID:            "b4eedccc6fb74fa8a7ad6b08382b852b",
    89  		Name:                "ipsecpolicy1",
    90  		TransformProtocol:   "esp",
    91  		AuthAlgorithm:       "sha1",
    92  		EncapsulationMode:   "tunnel",
    93  		EncryptionAlgorithm: "aes-128",
    94  		PFS:                 "group5",
    95  		Description:         "",
    96  		Lifetime:            expectedLifetime,
    97  		ID:                  "5291b189-fd84-46e5-84bd-78f40c05d69c",
    98  		ProjectID:           "b4eedccc6fb74fa8a7ad6b08382b852b",
    99  	}
   100  	th.AssertDeepEquals(t, expected, *actual)
   101  }
   102  
   103  func TestGet(t *testing.T) {
   104  	th.SetupHTTP()
   105  	defer th.TeardownHTTP()
   106  
   107  	th.Mux.HandleFunc("/v2.0/vpn/ipsecpolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   108  		th.TestMethod(t, r, "GET")
   109  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   110  
   111  		w.Header().Add("Content-Type", "application/json")
   112  		w.WriteHeader(http.StatusOK)
   113  
   114  		fmt.Fprint(w, `
   115  {
   116      "ipsecpolicy": {
   117          "name": "ipsecpolicy1",
   118          "transform_protocol": "esp",
   119          "auth_algorithm": "sha1",
   120          "encapsulation_mode": "tunnel",
   121          "encryption_algorithm": "aes-128",
   122          "pfs": "group5",
   123          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   124          "project_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   125          "lifetime": {
   126              "units": "seconds",
   127              "value": 7200
   128          },
   129          "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   130          "description": ""
   131      }
   132  }
   133          `)
   134  	})
   135  
   136  	actual, err := ipsecpolicies.Get(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract()
   137  	th.AssertNoErr(t, err)
   138  	expectedLifetime := ipsecpolicies.Lifetime{
   139  		Units: "seconds",
   140  		Value: 7200,
   141  	}
   142  	expected := ipsecpolicies.Policy{
   143  		Name:                "ipsecpolicy1",
   144  		TransformProtocol:   "esp",
   145  		Description:         "",
   146  		AuthAlgorithm:       "sha1",
   147  		EncapsulationMode:   "tunnel",
   148  		EncryptionAlgorithm: "aes-128",
   149  		PFS:                 "group5",
   150  		Lifetime:            expectedLifetime,
   151  		TenantID:            "b4eedccc6fb74fa8a7ad6b08382b852b",
   152  		ID:                  "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   153  		ProjectID:           "b4eedccc6fb74fa8a7ad6b08382b852b",
   154  	}
   155  	th.AssertDeepEquals(t, expected, *actual)
   156  }
   157  
   158  func TestDelete(t *testing.T) {
   159  	th.SetupHTTP()
   160  	defer th.TeardownHTTP()
   161  
   162  	th.Mux.HandleFunc("/v2.0/vpn/ipsecpolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   163  		th.TestMethod(t, r, "DELETE")
   164  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   165  		w.WriteHeader(http.StatusNoContent)
   166  	})
   167  
   168  	res := ipsecpolicies.Delete(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828")
   169  	th.AssertNoErr(t, res.Err)
   170  }
   171  
   172  func TestList(t *testing.T) {
   173  	th.SetupHTTP()
   174  	defer th.TeardownHTTP()
   175  
   176  	th.Mux.HandleFunc("/v2.0/vpn/ipsecpolicies", func(w http.ResponseWriter, r *http.Request) {
   177  		th.TestMethod(t, r, "GET")
   178  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   179  
   180  		w.Header().Add("Content-Type", "application/json")
   181  		w.WriteHeader(http.StatusOK)
   182  
   183  		fmt.Fprint(w, `
   184  		{
   185  	"ipsecpolicies": [
   186  		{
   187  			"name": "ipsecpolicy1",
   188  			"transform_protocol": "esp",
   189  			"auth_algorithm": "sha1",
   190  			"encapsulation_mode": "tunnel",
   191  			"encryption_algorithm": "aes-128",
   192  			"pfs": "group5",
   193  			"project_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   194  			"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   195  			"lifetime": {
   196  				"units": "seconds",
   197  				"value": 7200
   198  			},
   199  			"id": "5291b189-fd84-46e5-84bd-78f40c05d69c",
   200  			"description": ""
   201  		}
   202  	]
   203  }
   204  	  `)
   205  	})
   206  
   207  	count := 0
   208  
   209  	err := ipsecpolicies.List(fake.ServiceClient(), ipsecpolicies.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   210  		count++
   211  		actual, err := ipsecpolicies.ExtractPolicies(page)
   212  		if err != nil {
   213  			t.Errorf("Failed to extract members: %v", err)
   214  			return false, err
   215  		}
   216  
   217  		expected := []ipsecpolicies.Policy{
   218  			{
   219  				Name:                "ipsecpolicy1",
   220  				TransformProtocol:   "esp",
   221  				TenantID:            "b4eedccc6fb74fa8a7ad6b08382b852b",
   222  				ProjectID:           "b4eedccc6fb74fa8a7ad6b08382b852b",
   223  				AuthAlgorithm:       "sha1",
   224  				EncapsulationMode:   "tunnel",
   225  				EncryptionAlgorithm: "aes-128",
   226  				PFS:                 "group5",
   227  				Lifetime: ipsecpolicies.Lifetime{
   228  					Value: 7200,
   229  					Units: "seconds",
   230  				},
   231  				Description: "",
   232  				ID:          "5291b189-fd84-46e5-84bd-78f40c05d69c",
   233  			},
   234  		}
   235  
   236  		th.CheckDeepEquals(t, expected, actual)
   237  
   238  		return true, nil
   239  	})
   240  	th.AssertNoErr(t, err)
   241  
   242  	if count != 1 {
   243  		t.Errorf("Expected 1 page, got %d", count)
   244  	}
   245  }
   246  
   247  func TestUpdate(t *testing.T) {
   248  	th.SetupHTTP()
   249  	defer th.TeardownHTTP()
   250  
   251  	th.Mux.HandleFunc("/v2.0/vpn/ipsecpolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   252  		th.TestMethod(t, r, "PUT")
   253  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   254  		th.TestHeader(t, r, "Content-Type", "application/json")
   255  		th.TestHeader(t, r, "Accept", "application/json")
   256  		th.TestJSONRequest(t, r, `
   257  	{
   258  		"ipsecpolicy":{
   259  			"name": "updatedname",
   260  			"description": "updated policy",
   261  			"lifetime": {
   262  			"value": 7000
   263  			}
   264  		}
   265  	}
   266  	`)
   267  
   268  		w.Header().Add("Content-Type", "application/json")
   269  		w.WriteHeader(http.StatusOK)
   270  
   271  		fmt.Fprint(w, `
   272  
   273  	{
   274  		"ipsecpolicy": {
   275  		"name": "updatedname",
   276  		"transform_protocol": "esp",
   277  		"auth_algorithm": "sha1",
   278  		"encapsulation_mode": "tunnel",
   279  		"encryption_algorithm": "aes-128",
   280  		"pfs": "group5",
   281  		"project_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   282  		"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   283  		"lifetime": {
   284  			"units": "seconds",
   285  			"value": 7000
   286  		},
   287  		"id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   288  		"description": "updated policy"
   289  	}
   290  }
   291  `)
   292  	})
   293  	updatedName := "updatedname"
   294  	updatedDescription := "updated policy"
   295  	options := ipsecpolicies.UpdateOpts{
   296  		Name:        &updatedName,
   297  		Description: &updatedDescription,
   298  		Lifetime: &ipsecpolicies.LifetimeUpdateOpts{
   299  			Value: 7000,
   300  		},
   301  	}
   302  
   303  	actual, err := ipsecpolicies.Update(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract()
   304  	th.AssertNoErr(t, err)
   305  	expectedLifetime := ipsecpolicies.Lifetime{
   306  		Units: "seconds",
   307  		Value: 7000,
   308  	}
   309  	expected := ipsecpolicies.Policy{
   310  		TenantID:            "b4eedccc6fb74fa8a7ad6b08382b852b",
   311  		ProjectID:           "b4eedccc6fb74fa8a7ad6b08382b852b",
   312  		Name:                "updatedname",
   313  		TransformProtocol:   "esp",
   314  		AuthAlgorithm:       "sha1",
   315  		EncapsulationMode:   "tunnel",
   316  		EncryptionAlgorithm: "aes-128",
   317  		PFS:                 "group5",
   318  		Description:         "updated policy",
   319  		Lifetime:            expectedLifetime,
   320  		ID:                  "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   321  	}
   322  	th.AssertDeepEquals(t, expected, *actual)
   323  }