github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/projects/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  	th "github.com/gophercloud/gophercloud/testhelper"
     9  	"github.com/gophercloud/gophercloud/testhelper/client"
    10  )
    11  
    12  func TestListAvailableProjects(t *testing.T) {
    13  	th.SetupHTTP()
    14  	defer th.TeardownHTTP()
    15  	HandleListAvailableProjectsSuccessfully(t)
    16  
    17  	count := 0
    18  	err := projects.ListAvailable(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
    19  		count++
    20  
    21  		actual, err := projects.ExtractProjects(page)
    22  		th.AssertNoErr(t, err)
    23  
    24  		th.CheckDeepEquals(t, ExpectedAvailableProjectsSlice, actual)
    25  
    26  		return true, nil
    27  	})
    28  	th.AssertNoErr(t, err)
    29  	th.CheckEquals(t, count, 1)
    30  }
    31  
    32  func TestListProjects(t *testing.T) {
    33  	th.SetupHTTP()
    34  	defer th.TeardownHTTP()
    35  	HandleListProjectsSuccessfully(t)
    36  
    37  	count := 0
    38  	err := projects.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
    39  		count++
    40  
    41  		actual, err := projects.ExtractProjects(page)
    42  		th.AssertNoErr(t, err)
    43  
    44  		th.CheckDeepEquals(t, ExpectedProjectSlice, actual)
    45  
    46  		return true, nil
    47  	})
    48  	th.AssertNoErr(t, err)
    49  	th.CheckEquals(t, count, 1)
    50  }
    51  
    52  func TestListGroupsFiltersCheck(t *testing.T) {
    53  	type test struct {
    54  		filterName string
    55  		wantErr    bool
    56  	}
    57  	tests := []test{
    58  		{"foo__contains", false},
    59  		{"foo", true},
    60  		{"foo_contains", true},
    61  		{"foo__", true},
    62  		{"__foo", true},
    63  	}
    64  
    65  	var listOpts projects.ListOpts
    66  	for _, _test := range tests {
    67  		listOpts.Filters = map[string]string{_test.filterName: "bar"}
    68  		_, err := listOpts.ToProjectListQuery()
    69  
    70  		if !_test.wantErr {
    71  			th.AssertNoErr(t, err)
    72  		} else {
    73  			switch _t := err.(type) {
    74  			case nil:
    75  				t.Fatal("error expected but got a nil")
    76  			case projects.InvalidListFilter:
    77  			default:
    78  				t.Fatalf("unexpected error type: [%T]", _t)
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func TestGetProject(t *testing.T) {
    85  	th.SetupHTTP()
    86  	defer th.TeardownHTTP()
    87  	HandleGetProjectSuccessfully(t)
    88  
    89  	actual, err := projects.Get(client.ServiceClient(), "1234").Extract()
    90  	th.AssertNoErr(t, err)
    91  	th.CheckDeepEquals(t, RedTeam, *actual)
    92  }
    93  
    94  func TestCreateProject(t *testing.T) {
    95  	th.SetupHTTP()
    96  	defer th.TeardownHTTP()
    97  	HandleCreateProjectSuccessfully(t)
    98  
    99  	createOpts := projects.CreateOpts{
   100  		Name:        "Red Team",
   101  		Description: "The team that is red",
   102  		Tags:        []string{"Red", "Team"},
   103  		Extra:       map[string]interface{}{"test": "old"},
   104  	}
   105  
   106  	actual, err := projects.Create(client.ServiceClient(), createOpts).Extract()
   107  	th.AssertNoErr(t, err)
   108  	th.CheckDeepEquals(t, RedTeam, *actual)
   109  }
   110  
   111  func TestDeleteProject(t *testing.T) {
   112  	th.SetupHTTP()
   113  	defer th.TeardownHTTP()
   114  	HandleDeleteProjectSuccessfully(t)
   115  
   116  	res := projects.Delete(client.ServiceClient(), "1234")
   117  	th.AssertNoErr(t, res.Err)
   118  }
   119  
   120  func TestUpdateProject(t *testing.T) {
   121  	th.SetupHTTP()
   122  	defer th.TeardownHTTP()
   123  	HandleUpdateProjectSuccessfully(t)
   124  
   125  	var description = "The team that is bright red"
   126  	updateOpts := projects.UpdateOpts{
   127  		Name:        "Bright Red Team",
   128  		Description: &description,
   129  		Tags:        &[]string{"Red"},
   130  		Extra:       map[string]interface{}{"test": "new"},
   131  	}
   132  
   133  	actual, err := projects.Update(client.ServiceClient(), "1234", updateOpts).Extract()
   134  	th.AssertNoErr(t, err)
   135  	th.CheckDeepEquals(t, UpdatedRedTeam, *actual)
   136  }