github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/swr/v2/organizations_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/swr/v2/organizations"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  )
    14  
    15  func randomRepoName(prefix string, n int) string {
    16  	const alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
    17  	var bytes = make([]byte, n)
    18  	_, _ = rand.Read(bytes)
    19  	for i, b := range bytes {
    20  		bytes[i] = alphanum[b%byte(len(alphanum))]
    21  	}
    22  	return prefix + string(bytes)
    23  }
    24  
    25  func TestOrganizationWorkflow(t *testing.T) {
    26  	client, err := clients.NewSwrV2Client()
    27  	th.AssertNoErr(t, err)
    28  
    29  	name := randomRepoName("test-org", 6)
    30  	opts := organizations.CreateOpts{Namespace: name}
    31  	err = organizations.Create(client, opts)
    32  	th.AssertNoErr(t, err)
    33  	t.Cleanup(func() {
    34  		th.AssertNoErr(t, organizations.Delete(client, name))
    35  	})
    36  
    37  	org, err := organizations.Get(client, name)
    38  	th.AssertNoErr(t, err)
    39  	th.AssertEquals(t, name, org.Name)
    40  
    41  	orgs, err := organizations.List(client, organizations.ListOpts{})
    42  	th.AssertNoErr(t, err)
    43  	found := false
    44  	for _, o := range orgs {
    45  		if o.Name == name {
    46  			found = true
    47  			break
    48  		}
    49  	}
    50  	if !found {
    51  		t.Fatalf("can't find organization '%s' in the list", name)
    52  	}
    53  }
    54  
    55  func TestOrganizationPermissionsWorkflow(t *testing.T) {
    56  	userID := os.Getenv("OS_USER_ID_2")
    57  	username := os.Getenv("OS_USERNAME_2")
    58  
    59  	if username == "" || userID == "" {
    60  		t.Skip("OS_USER_ID_2 and OS_USERNAME_2 should be set to test permission granting")
    61  	}
    62  
    63  	client, err := clients.NewSwrV2Client()
    64  	th.AssertNoErr(t, err)
    65  
    66  	// setup org
    67  	orgName := fmt.Sprintf("repo-test-%d", tools.RandomInt(0, 0xf))
    68  	dep := dependencies{t: t, client: client}
    69  	dep.createOrganization(orgName)
    70  	t.Cleanup(func() { dep.deleteOrganization(orgName) })
    71  
    72  	auth := organizations.Auth{
    73  		UserID:   userID,
    74  		Username: username,
    75  		Auth:     3,
    76  	}
    77  
    78  	err = organizations.CreatePermissions(client, orgName, []organizations.Auth{auth})
    79  	th.AssertNoErr(t, err)
    80  
    81  	t.Cleanup(func() {
    82  		err := organizations.DeletePermissions(client, orgName, auth.UserID)
    83  		th.AssertNoErr(t, err)
    84  		perms, err := organizations.GetPermissions(client, orgName)
    85  		th.AssertNoErr(t, err)
    86  		assertAuthNotInPermissions(t, auth, perms)
    87  	})
    88  
    89  	perms, err := organizations.GetPermissions(client, orgName)
    90  	th.AssertNoErr(t, err)
    91  	assertAuthInPermissions(t, auth, perms)
    92  
    93  	newAuth := organizations.Auth{
    94  		UserID:   auth.UserID,
    95  		Username: auth.Username,
    96  		Auth:     1,
    97  	}
    98  	err = organizations.UpdatePermissions(client, orgName, []organizations.Auth{newAuth})
    99  	th.AssertNoErr(t, err)
   100  
   101  	updatedPerms, err := organizations.GetPermissions(client, orgName)
   102  	th.AssertNoErr(t, err)
   103  	assertAuthInPermissions(t, newAuth, updatedPerms)
   104  }
   105  
   106  func assertAuthInPermissions(t *testing.T, expected organizations.Auth, actual *organizations.Permissions) {
   107  	if actual == nil {
   108  		t.Fatal("actual organization permissions are nil")
   109  	}
   110  
   111  	for _, a := range actual.OthersAuth {
   112  		if a.UserID == expected.UserID {
   113  			if a.Username != expected.Username {
   114  				t.Fatalf(
   115  					"user ID is the same, but username differ - this is unexpected (%s/%s instead of %[1]s/%s)",
   116  					a.UserID, a.Username, expected.Username,
   117  				)
   118  			}
   119  			if a.Auth != expected.Auth {
   120  				t.Fatalf("auth was %d, but %d expected", a.Auth, expected.Auth)
   121  			}
   122  			return
   123  		}
   124  	}
   125  	t.Fatal("expected permission is not found in the `others_auth` list")
   126  }
   127  
   128  func assertAuthNotInPermissions(t *testing.T, expected organizations.Auth, actual *organizations.Permissions) {
   129  	if actual == nil {
   130  		t.Fatal("actual organization permissions are nil")
   131  	}
   132  
   133  	for _, a := range actual.OthersAuth {
   134  		if a.UserID == expected.UserID {
   135  			t.Fatalf("expected permission to be deleted, but it exist")
   136  		}
   137  	}
   138  }