github.com/minio/console@v1.3.0/integration/groups_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package integration
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/base64"
    22  	"encoding/json"
    23  	"fmt"
    24  	"log"
    25  	"net/http"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func Test_AddGroupAPI(t *testing.T) {
    33  	assert := assert.New(t)
    34  
    35  	AddUser("member1", "testtest", []string{}, []string{"consoleAdmin"})
    36  
    37  	type args struct {
    38  		group   string
    39  		members []string
    40  	}
    41  	tests := []struct {
    42  		name           string
    43  		args           args
    44  		expectedStatus int
    45  		expectedError  error
    46  	}{
    47  		{
    48  			name: "Create Group - Valid",
    49  			args: args{
    50  				group:   "test",
    51  				members: []string{"member1"},
    52  			},
    53  			expectedStatus: 201,
    54  			expectedError:  nil,
    55  		},
    56  		{
    57  			name: "Create Group - Invalid",
    58  			args: args{
    59  				group:   "test",
    60  				members: []string{},
    61  			},
    62  			expectedStatus: 400,
    63  			expectedError:  nil,
    64  		},
    65  	}
    66  
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(_ *testing.T) {
    69  			client := &http.Client{
    70  				Timeout: 3 * time.Second,
    71  			}
    72  
    73  			requestDataPolicy := map[string]interface{}{}
    74  			requestDataPolicy["group"] = tt.args.group
    75  			requestDataPolicy["members"] = tt.args.members
    76  
    77  			requestDataJSON, _ := json.Marshal(requestDataPolicy)
    78  			requestDataBody := bytes.NewReader(requestDataJSON)
    79  			request, err := http.NewRequest(
    80  				"POST", "http://localhost:9090/api/v1/groups", requestDataBody)
    81  			if err != nil {
    82  				log.Println(err)
    83  				return
    84  			}
    85  			request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
    86  			request.Header.Add("Content-Type", "application/json")
    87  			response, err := client.Do(request)
    88  			if err != nil {
    89  				log.Println(err)
    90  				return
    91  			}
    92  			if response != nil {
    93  				assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func Test_GetGroupAPI(t *testing.T) {
   100  	assert := assert.New(t)
   101  
   102  	AddUser("member2", "testtest", []string{}, []string{"consoleAdmin"})
   103  	AddGroup("getgroup1", []string{"member2"})
   104  
   105  	type args struct {
   106  		api string
   107  	}
   108  	tests := []struct {
   109  		name           string
   110  		args           args
   111  		expectedStatus int
   112  		expectedError  error
   113  	}{
   114  		{
   115  			name: "Get Group - Valid",
   116  			args: args{
   117  				api: base64.StdEncoding.EncodeToString([]byte("getgroup1")),
   118  			},
   119  			expectedStatus: 200,
   120  			expectedError:  nil,
   121  		},
   122  		{
   123  			name: "Get Group - Invalid",
   124  			args: args{
   125  				api: base64.StdEncoding.EncodeToString([]byte("askfjalkd")),
   126  			},
   127  			expectedStatus: 500,
   128  			expectedError:  nil,
   129  		},
   130  	}
   131  
   132  	for _, tt := range tests {
   133  		t.Run(tt.name, func(_ *testing.T) {
   134  			client := &http.Client{
   135  				Timeout: 3 * time.Second,
   136  			}
   137  
   138  			requestDataPolicy := map[string]interface{}{}
   139  
   140  			requestDataJSON, _ := json.Marshal(requestDataPolicy)
   141  			requestDataBody := bytes.NewReader(requestDataJSON)
   142  			request, err := http.NewRequest(
   143  				"GET", fmt.Sprintf("http://localhost:9090/api/v1/group/%s", tt.args.api), requestDataBody)
   144  			if err != nil {
   145  				log.Println(err)
   146  				return
   147  			}
   148  			request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
   149  			request.Header.Add("Content-Type", "application/json")
   150  			response, err := client.Do(request)
   151  			if err != nil {
   152  				log.Println(err)
   153  				return
   154  			}
   155  			if response != nil {
   156  				assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func Test_ListGroupsAPI(t *testing.T) {
   163  	assert := assert.New(t)
   164  
   165  	tests := []struct {
   166  		name           string
   167  		expectedStatus int
   168  		expectedError  error
   169  	}{
   170  		{
   171  			name:           "Get Group - Valid",
   172  			expectedStatus: 200,
   173  			expectedError:  nil,
   174  		},
   175  	}
   176  
   177  	for _, tt := range tests {
   178  		t.Run(tt.name, func(_ *testing.T) {
   179  			client := &http.Client{
   180  				Timeout: 3 * time.Second,
   181  			}
   182  
   183  			requestDataPolicy := map[string]interface{}{}
   184  
   185  			requestDataJSON, _ := json.Marshal(requestDataPolicy)
   186  			requestDataBody := bytes.NewReader(requestDataJSON)
   187  			request, err := http.NewRequest(
   188  				"GET", "http://localhost:9090/api/v1/groups", requestDataBody)
   189  			if err != nil {
   190  				log.Println(err)
   191  				return
   192  			}
   193  			request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
   194  			request.Header.Add("Content-Type", "application/json")
   195  			response, err := client.Do(request)
   196  			if err != nil {
   197  				log.Println(err)
   198  				return
   199  			}
   200  			if response != nil {
   201  				assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
   202  			}
   203  		})
   204  	}
   205  }
   206  
   207  func Test_PutGroupsAPI(t *testing.T) {
   208  	assert := assert.New(t)
   209  
   210  	AddUser("member3", "testtest", []string{}, []string{"consoleAdmin"})
   211  	AddGroup("putgroup1", []string{})
   212  
   213  	type args struct {
   214  		api     string
   215  		members []string
   216  		status  string
   217  	}
   218  	tests := []struct {
   219  		name           string
   220  		args           args
   221  		expectedStatus int
   222  		expectedError  error
   223  	}{
   224  		{
   225  			name: "Put Group - Valid",
   226  			args: args{
   227  				api:     base64.StdEncoding.EncodeToString([]byte("putgroup1")),
   228  				members: []string{"member3"},
   229  				status:  "enabled",
   230  			},
   231  			expectedStatus: 200,
   232  			expectedError:  nil,
   233  		},
   234  		{
   235  			name: "Put Group - Invalid",
   236  			args: args{
   237  				api:     base64.StdEncoding.EncodeToString([]byte("gdgfdfgd")),
   238  				members: []string{"member3"},
   239  				status:  "enabled",
   240  			},
   241  			expectedStatus: 500,
   242  			expectedError:  nil,
   243  		},
   244  	}
   245  
   246  	for _, tt := range tests {
   247  		t.Run(tt.name, func(_ *testing.T) {
   248  			client := &http.Client{
   249  				Timeout: 3 * time.Second,
   250  			}
   251  
   252  			requestDataPolicy := map[string]interface{}{}
   253  
   254  			requestDataPolicy["members"] = tt.args.members
   255  			requestDataPolicy["status"] = tt.args.status
   256  
   257  			requestDataJSON, _ := json.Marshal(requestDataPolicy)
   258  			requestDataBody := bytes.NewReader(requestDataJSON)
   259  			request, err := http.NewRequest(
   260  				"PUT", fmt.Sprintf("http://localhost:9090/api/v1/group/%s", tt.args.api), requestDataBody)
   261  			if err != nil {
   262  				log.Println(err)
   263  				return
   264  			}
   265  			request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
   266  			request.Header.Add("Content-Type", "application/json")
   267  			response, err := client.Do(request)
   268  			if err != nil {
   269  				log.Println(err)
   270  				return
   271  			}
   272  			if response != nil {
   273  				assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
   274  			}
   275  		})
   276  	}
   277  }
   278  
   279  func Test_DeleteGroupAPI(t *testing.T) {
   280  	assert := assert.New(t)
   281  
   282  	AddGroup("grouptests1", []string{})
   283  
   284  	type args struct {
   285  		api string
   286  	}
   287  	tests := []struct {
   288  		name           string
   289  		args           args
   290  		expectedStatus int
   291  		expectedError  error
   292  		verb           string
   293  	}{
   294  		{
   295  			name: "Delete Group - Valid",
   296  			args: args{
   297  				api: base64.StdEncoding.EncodeToString([]byte("grouptests1")),
   298  			},
   299  			verb:           "DELETE",
   300  			expectedStatus: 204,
   301  			expectedError:  nil,
   302  		},
   303  		{
   304  			name: "Delete Group - Invalid",
   305  			args: args{
   306  				api: base64.StdEncoding.EncodeToString([]byte("grouptests12345")),
   307  			},
   308  			verb:           "DELETE",
   309  			expectedStatus: 404,
   310  			expectedError:  nil,
   311  		},
   312  		{
   313  			name: "Access Group After Delete - Invalid",
   314  			args: args{
   315  				api: base64.StdEncoding.EncodeToString([]byte("grouptests1")),
   316  			},
   317  			verb:           "GET",
   318  			expectedStatus: 500,
   319  			expectedError:  nil,
   320  		},
   321  	}
   322  
   323  	for _, tt := range tests {
   324  		t.Run(tt.name, func(_ *testing.T) {
   325  			client := &http.Client{
   326  				Timeout: 3 * time.Second,
   327  			}
   328  
   329  			requestDataPolicy := map[string]interface{}{}
   330  
   331  			requestDataJSON, _ := json.Marshal(requestDataPolicy)
   332  			requestDataBody := bytes.NewReader(requestDataJSON)
   333  			request, err := http.NewRequest(
   334  				tt.verb, fmt.Sprintf("http://localhost:9090/api/v1/group/%s", tt.args.api), requestDataBody)
   335  			if err != nil {
   336  				log.Println(err)
   337  				return
   338  			}
   339  			request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
   340  			request.Header.Add("Content-Type", "application/json")
   341  			response, err := client.Do(request)
   342  			if err != nil {
   343  				log.Println(err)
   344  				return
   345  			}
   346  			if response != nil {
   347  				assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect")
   348  			}
   349  		})
   350  	}
   351  }