github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/users_test.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"net/http"
     8  	"strings"
     9  	"sync"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  var (
    15  	set_user  sync.Once
    16  	getc_user sync.Once
    17  	tuname    string
    18  	tudesc    string
    19  	tumail    string
    20  	tu_id     string
    21  	cur_user  *User
    22  	test_user *User
    23  )
    24  
    25  // Helper functions
    26  
    27  func create_user() *User {
    28  	rand.Seed(time.Now().UnixNano())
    29  	r := rand.Intn(999999)
    30  	tuname = fmt.Sprintf("testuser_%d", r)
    31  	tudesc = fmt.Sprintf("testuser_%d description", r)
    32  	tumail = "testuser@1and1.com"
    33  	req := UserRequest{
    34  		Name:        tuname,
    35  		Description: tudesc,
    36  		Password:    fmt.Sprintf("Ss&)Hg3@&9!hJ&5%d", r),
    37  		Email:       tumail,
    38  	}
    39  	fmt.Printf("Creating new user '%s'...\n", tuname)
    40  	u_id, u, err := api.CreateUser(&req)
    41  	if err != nil {
    42  		fmt.Printf("Unable to create a user. Error: %s", err.Error())
    43  		return nil
    44  	}
    45  	if u_id == "" || u.Id == "" {
    46  		fmt.Printf("Unable to create user '%s'.", tuname)
    47  		return nil
    48  	}
    49  
    50  	return u
    51  }
    52  
    53  func get_my_pub_ip() string {
    54  	res, err := http.Get("http://echoip.com/")
    55  	if err != nil {
    56  		return ""
    57  	}
    58  	defer res.Body.Close()
    59  	ip, err := ioutil.ReadAll(res.Body)
    60  	if err != nil {
    61  		return ""
    62  	}
    63  	return string(ip)
    64  }
    65  
    66  func set_test_users() {
    67  	test_user = create_user()
    68  }
    69  
    70  func get_current_user() {
    71  	res, _ := api.ListUsers()
    72  
    73  	if res != nil {
    74  		for _, u := range res {
    75  			if Token == u.Api.Key {
    76  				cur_user = &u
    77  				break
    78  			}
    79  		}
    80  	}
    81  }
    82  
    83  // /users tests
    84  
    85  func TestCreateUser(t *testing.T) {
    86  	t.Skip("TestCreateUser is skipped at the moment.")
    87  	set_user.Do(set_test_users)
    88  
    89  	if test_user == nil {
    90  		t.Errorf("CreateUser failed.")
    91  		return
    92  	}
    93  	if !strings.Contains(test_user.Name, tuname) {
    94  		t.Errorf("Wrong user name.")
    95  	}
    96  	if test_user.Description != tudesc {
    97  		t.Errorf("Wrong user description.")
    98  	}
    99  	if test_user.Email != tumail {
   100  		t.Errorf("Wrong user email.")
   101  	}
   102  }
   103  
   104  func TestListUsers(t *testing.T) {
   105  	//	set_user.Do(set_test_users)
   106  	fmt.Println("Listing all users...")
   107  
   108  	res, err := api.ListUsers()
   109  	if err != nil {
   110  		t.Errorf("ListUsers failed. Error: " + err.Error())
   111  	}
   112  	if len(res) == 0 {
   113  		t.Errorf("No user found.")
   114  	}
   115  
   116  	for _, u := range res {
   117  		if Token == u.Api.Key {
   118  			cur_user = &u
   119  			break
   120  		}
   121  	}
   122  
   123  	res, err = api.ListUsers(1, 3, "name", "", "id,name")
   124  
   125  	if err != nil {
   126  		t.Errorf("ListUsers with parameter options failed. Error: " + err.Error())
   127  		return
   128  	}
   129  	if len(res) == 0 {
   130  		t.Errorf("No user found.")
   131  	}
   132  	if len(res) != 3 {
   133  		t.Errorf("Wrong number of objects per page.")
   134  	}
   135  
   136  	for i, _ := range res {
   137  		if res[i].Id == "" || res[i].Name == "" || res[i].State != "" ||
   138  			res[i].Api != nil || res[i].Role != nil {
   139  			t.Errorf("Filtering parameters failed.")
   140  		}
   141  		if i < len(res)-1 {
   142  			if res[i].Name > res[i+1].Name {
   143  				t.Errorf("Sorting list of users failed.")
   144  			}
   145  		}
   146  	}
   147  	// Test for error response
   148  	res, err = api.ListUsers("", nil, 10, "15", "")
   149  	if res != nil || err == nil {
   150  		t.Errorf("ListUsers failed to handle incorrect argument type.")
   151  	}
   152  
   153  	res, err = api.ListUsers(0, 0, "", cur_user.Name, "")
   154  
   155  	if err != nil {
   156  		t.Errorf("ListUsers with parameter options failed. Error: " + err.Error())
   157  		return
   158  	}
   159  	if len(res) != 1 {
   160  		t.Errorf("Search parameter failed.")
   161  	}
   162  	if res[0].Name != cur_user.Name {
   163  		t.Errorf("Search parameter failed.")
   164  	}
   165  }
   166  
   167  func TestGetUser(t *testing.T) {
   168  	getc_user.Do(get_current_user)
   169  
   170  	fmt.Printf("Getting user '%s'...\n", cur_user.Name)
   171  	u, err := api.GetUser(cur_user.Id)
   172  
   173  	if err != nil {
   174  		t.Errorf("GetUser failed. Error: " + err.Error())
   175  	} else {
   176  		if u.Id != cur_user.Id {
   177  			t.Errorf("Wrong user ID.")
   178  		}
   179  	}
   180  }
   181  
   182  func TestGetCurrentUserPermissions(t *testing.T) {
   183  	getc_user.Do(get_current_user)
   184  
   185  	fmt.Printf("Getting current user permissions ...\n")
   186  	p, err := api.GetCurrentUserPermissions()
   187  
   188  	if err != nil {
   189  		t.Errorf("GetCurrentUserPermissions failed. Error: " + err.Error())
   190  	} else {
   191  		if p == nil || p.Backups == nil || p.Firewalls == nil || p.Images == nil || p.Invoice == nil ||
   192  			p.IPs == nil || p.LoadBalancers == nil || p.Logs == nil || p.MonitorCenter == nil ||
   193  			p.MonitorPolicies == nil || p.PrivateNetworks == nil || p.Roles == nil || p.Servers == nil ||
   194  			p.SharedStorage == nil || p.Usages == nil || p.Users == nil || p.VPNs == nil {
   195  			t.Errorf("Some permissions objects are missing.")
   196  		}
   197  	}
   198  }
   199  
   200  func TestGetUserApi(t *testing.T) {
   201  	getc_user.Do(get_current_user)
   202  
   203  	fmt.Printf("Getting API data of user '%s'...\n", cur_user.Name)
   204  	ua, err := api.GetUserApi(cur_user.Id)
   205  
   206  	if err != nil {
   207  		t.Errorf("GetUserApi failed. Error: " + err.Error())
   208  	} else {
   209  		if ua.Key != cur_user.Api.Key {
   210  			t.Errorf("Wrong user key.")
   211  		}
   212  		if !ua.Active {
   213  			t.Errorf("Wrong user active state.")
   214  		}
   215  	}
   216  }
   217  
   218  func TestModifyUserApi(t *testing.T) {
   219  	getc_user.Do(get_current_user)
   220  
   221  	fmt.Printf("Modify API state of user '%s'...\n", cur_user.Name)
   222  	// Just making sure that the request pass.
   223  	// TODO: test with active=false once the REST functionality is completed.
   224  	u, err := api.ModifyUserApi(cur_user.Id, true)
   225  
   226  	if err != nil {
   227  		t.Errorf("ModifyUserApi failed. Error: " + err.Error())
   228  	} else {
   229  		if u.Api.Key != cur_user.Api.Key {
   230  			t.Errorf("Wrong user key.")
   231  		}
   232  		if !u.Api.Active {
   233  			t.Errorf("Wrong user active state.")
   234  		}
   235  	}
   236  }
   237  
   238  func TestGetUserApiKey(t *testing.T) {
   239  	getc_user.Do(get_current_user)
   240  
   241  	fmt.Printf("Getting API data of user '%s'...\n", cur_user.Name)
   242  	key, err := api.GetUserApiKey(cur_user.Id)
   243  
   244  	if err != nil {
   245  		t.Errorf("GetUserApiKey failed. Error: " + err.Error())
   246  	} else {
   247  		if key.Key != cur_user.Api.Key {
   248  			t.Errorf("Wrong user key.")
   249  		}
   250  	}
   251  }
   252  
   253  func TestAddUserApiAlowedIps(t *testing.T) {
   254  	getc_user.Do(get_current_user)
   255  
   256  	fmt.Printf("Adding API allowed IPs to user '%s'...\n", cur_user.Name)
   257  	my_ip := get_my_pub_ip()
   258  	if my_ip == "" {
   259  		fmt.Println("Not able to obtain its own public IP. Skipping the test.")
   260  		return
   261  	}
   262  	ips := []string{my_ip, "192.168.7.77", "10.81.12.101"}
   263  	u, err := api.AddUserApiAlowedIps(cur_user.Id, ips)
   264  
   265  	if err != nil {
   266  		t.Errorf("AddUserApiAlowedIps failed. Error: " + err.Error())
   267  	} else {
   268  		if len(u.Api.AllowedIps) != 3 {
   269  			t.Errorf("Unable to add API allowed IPs to the user.")
   270  		}
   271  		for _, a := range u.Api.AllowedIps {
   272  			if a != my_ip && a != "192.168.7.77" && a != "10.81.12.101" {
   273  				t.Errorf("Wrong IP added to user's API allowed list.")
   274  			}
   275  		}
   276  	}
   277  }
   278  
   279  func TestListUserApiAllowedIps(t *testing.T) {
   280  	getc_user.Do(get_current_user)
   281  
   282  	fmt.Printf("Listing API allowed IPs to user '%s'...\n", cur_user.Name)
   283  	ips, err := api.ListUserApiAllowedIps(cur_user.Id)
   284  
   285  	if err != nil {
   286  		t.Errorf("ListUserApiAllowedIps failed. Error: " + err.Error())
   287  	} else {
   288  		if len(ips) != 3 {
   289  			t.Errorf("Wrong number of API allowed IPs found.")
   290  		}
   291  	}
   292  }
   293  
   294  func TestRemoveUserApiAllowedIp(t *testing.T) {
   295  	getc_user.Do(get_current_user)
   296  
   297  	fmt.Printf("Removing API allowed IPs to user '%s'...\n", cur_user.Name)
   298  	my_ip := get_my_pub_ip()
   299  	if my_ip == "" {
   300  		fmt.Println("Not able to obtain its own public IP. Skipping the test.")
   301  		return
   302  	}
   303  	ips := []string{"192.168.7.77", "10.81.12.101", my_ip}
   304  
   305  	for _, ip := range ips {
   306  		_, err := api.RemoveUserApiAllowedIp(cur_user.Id, ip)
   307  
   308  		if err != nil {
   309  			t.Errorf("RemoveUserApiAllowedIp failed. Error: " + err.Error())
   310  		}
   311  	}
   312  	u, _ := api.GetUser(cur_user.Id)
   313  	if len(u.Api.AllowedIps) != 0 {
   314  		t.Errorf("RemoveUserApiAllowedIp failed.")
   315  	}
   316  }
   317  
   318  func TestRenewUserApiKey(t *testing.T) {
   319  	t.Skip("TestRenewUserApiKey is skipped at the moment.")
   320  	getc_user.Do(get_current_user)
   321  
   322  	fmt.Printf("Renewing API key of user '%s'...\n", cur_user.Name)
   323  	u, err := api.RenewUserApiKey(cur_user.Id)
   324  
   325  	if err != nil {
   326  		t.Errorf("RenewUserApiKey failed. Error: " + err.Error())
   327  	} else {
   328  		if u.Api.Key == cur_user.Api.Key {
   329  			t.Errorf("Unable to renew user key.")
   330  		} else {
   331  			cur_user = u
   332  			api = New(u.Api.Key, BaseUrl)
   333  			SetToken(u.Api.Key)
   334  			setEnvironmentVar("ONEANDONE_TOKEN", u.Api.Key)
   335  		}
   336  	}
   337  }
   338  
   339  func TestModifyUser(t *testing.T) {
   340  	getc_user.Do(get_current_user)
   341  
   342  	fmt.Printf("Modifying user '%s'...\n", cur_user.Name)
   343  	rand.Seed(time.Now().UnixNano())
   344  	r := rand.Intn(999999)
   345  	new_pass := fmt.Sprintf("%d^&*bbYhgf%djv;HF", r, r)
   346  	new_desc := tudesc + "_updated"
   347  	new_mail := "test@oneandone.com"
   348  	req := UserRequest{
   349  		Description: new_desc,
   350  		Email:       new_mail,
   351  		Password:    new_pass,
   352  		//		State: "DISABLED",
   353  	}
   354  	u, err := api.ModifyUser(cur_user.Id, &req)
   355  
   356  	if err != nil {
   357  		t.Errorf("ModifyUser failed. Error: " + err.Error())
   358  		return
   359  	}
   360  
   361  	if u.Description != new_desc {
   362  		t.Errorf("User description not updated.")
   363  	}
   364  	if u.Email != new_mail {
   365  		t.Errorf("User email not updated.")
   366  	}
   367  }
   368  
   369  func TestDeleteUser(t *testing.T) {
   370  	t.Skip("TestDeleteUser is skipped at the moment.")
   371  	getc_user.Do(get_current_user)
   372  
   373  	fmt.Printf("Deleting user '%s'...\n", cur_user.Name)
   374  	u, err := api.DeleteUser(cur_user.Id)
   375  
   376  	if err != nil {
   377  		t.Errorf("DeleteUser failed. Error: " + err.Error())
   378  		return
   379  	}
   380  	if u != nil {
   381  		u, err = api.GetUser(u.Id)
   382  
   383  		if u != nil {
   384  			t.Errorf("Unable to delete the user.")
   385  		} else {
   386  			cur_user = nil
   387  		}
   388  	}
   389  }