github.com/minio/console@v1.3.0/pkg/subnet/utils_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2023 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 subnet
    18  
    19  import (
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/minio/mc/cmd"
    25  )
    26  
    27  func Test_subnetBaseURL(t *testing.T) {
    28  	type args struct {
    29  		env map[string]string
    30  	}
    31  	tests := []struct {
    32  		name string
    33  		args args
    34  		want string
    35  	}{
    36  		{
    37  			name: "default",
    38  			args: args{
    39  				env: nil,
    40  			},
    41  			want: "https://subnet.min.io",
    42  		},
    43  		{
    44  			name: "with env",
    45  			args: args{
    46  				env: map[string]string{
    47  					"CONSOLE_SUBNET_URL": "http://oorgle",
    48  				},
    49  			},
    50  			want: "http://oorgle",
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(_ *testing.T) {
    55  			if tt.args.env != nil {
    56  				for k, v := range tt.args.env {
    57  					os.Setenv(k, v)
    58  				}
    59  			}
    60  			if got := subnetBaseURL(); got != tt.want {
    61  				t.Errorf("subnetBaseURL() = %v, want %v", got, tt.want)
    62  			}
    63  			if tt.args.env != nil {
    64  				for k := range tt.args.env {
    65  					os.Unsetenv(k)
    66  				}
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func Test_subnetRegisterURL(t *testing.T) {
    73  	type args struct {
    74  		env map[string]string
    75  	}
    76  	tests := []struct {
    77  		name string
    78  		args args
    79  		want string
    80  	}{
    81  		{
    82  			name: "default",
    83  			args: args{
    84  				env: nil,
    85  			},
    86  			want: "https://subnet.min.io/api/cluster/register",
    87  		},
    88  		{
    89  			name: "with env",
    90  			args: args{
    91  				env: map[string]string{
    92  					"CONSOLE_SUBNET_URL": "http://oorgle",
    93  				},
    94  			},
    95  			want: "http://oorgle/api/cluster/register",
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		if tt.args.env != nil {
   100  			for k, v := range tt.args.env {
   101  				os.Setenv(k, v)
   102  			}
   103  		}
   104  		t.Run(tt.name, func(_ *testing.T) {
   105  			if got := subnetRegisterURL(); got != tt.want {
   106  				t.Errorf("subnetRegisterURL() = %v, want %v", got, tt.want)
   107  			}
   108  		})
   109  		if tt.args.env != nil {
   110  			for k := range tt.args.env {
   111  				os.Unsetenv(k)
   112  			}
   113  		}
   114  	}
   115  }
   116  
   117  func Test_subnetLoginURL(t *testing.T) {
   118  	type args struct {
   119  		env map[string]string
   120  	}
   121  	tests := []struct {
   122  		name string
   123  		args args
   124  		want string
   125  	}{
   126  		{
   127  			name: "default",
   128  			args: args{
   129  				env: nil,
   130  			},
   131  			want: "https://subnet.min.io/api/auth/login",
   132  		},
   133  		{
   134  			name: "with env",
   135  			args: args{
   136  				env: map[string]string{
   137  					"CONSOLE_SUBNET_URL": "http://oorgle",
   138  				},
   139  			},
   140  			want: "http://oorgle/api/auth/login",
   141  		},
   142  	}
   143  	for _, tt := range tests {
   144  		if tt.args.env != nil {
   145  			for k, v := range tt.args.env {
   146  				os.Setenv(k, v)
   147  			}
   148  		}
   149  		t.Run(tt.name, func(_ *testing.T) {
   150  			if got := subnetLoginURL(); got != tt.want {
   151  				t.Errorf("subnetLoginURL() = %v, want %v", got, tt.want)
   152  			}
   153  		})
   154  		if tt.args.env != nil {
   155  			for k := range tt.args.env {
   156  				os.Unsetenv(k)
   157  			}
   158  		}
   159  	}
   160  }
   161  
   162  func Test_subnetOrgsURL(t *testing.T) {
   163  	type args struct {
   164  		env map[string]string
   165  	}
   166  	tests := []struct {
   167  		name string
   168  		args args
   169  		want string
   170  	}{
   171  		{
   172  			name: "default",
   173  			args: args{
   174  				env: nil,
   175  			},
   176  			want: "https://subnet.min.io/api/auth/organizations",
   177  		},
   178  		{
   179  			name: "with env",
   180  			args: args{
   181  				env: map[string]string{
   182  					"CONSOLE_SUBNET_URL": "http://oorgle",
   183  				},
   184  			},
   185  			want: "http://oorgle/api/auth/organizations",
   186  		},
   187  	}
   188  	for _, tt := range tests {
   189  		if tt.args.env != nil {
   190  			for k, v := range tt.args.env {
   191  				os.Setenv(k, v)
   192  			}
   193  		}
   194  		t.Run(tt.name, func(_ *testing.T) {
   195  			if got := subnetOrgsURL(); got != tt.want {
   196  				t.Errorf("subnetOrgsURL() = %v, want %v", got, tt.want)
   197  			}
   198  		})
   199  		if tt.args.env != nil {
   200  			for k := range tt.args.env {
   201  				os.Unsetenv(k)
   202  			}
   203  		}
   204  	}
   205  }
   206  
   207  func Test_subnetMFAURL(t *testing.T) {
   208  	type args struct {
   209  		env map[string]string
   210  	}
   211  	tests := []struct {
   212  		name string
   213  		args args
   214  		want string
   215  	}{
   216  		{
   217  			name: "default",
   218  			args: args{
   219  				env: nil,
   220  			},
   221  			want: "https://subnet.min.io/api/auth/mfa-login",
   222  		},
   223  		{
   224  			name: "with env",
   225  			args: args{
   226  				env: map[string]string{
   227  					"CONSOLE_SUBNET_URL": "http://oorgle",
   228  				},
   229  			},
   230  			want: "http://oorgle/api/auth/mfa-login",
   231  		},
   232  	}
   233  	for _, tt := range tests {
   234  		if tt.args.env != nil {
   235  			for k, v := range tt.args.env {
   236  				os.Setenv(k, v)
   237  			}
   238  		}
   239  		t.Run(tt.name, func(_ *testing.T) {
   240  			if got := subnetMFAURL(); got != tt.want {
   241  				t.Errorf("subnetMFAURL() = %v, want %v", got, tt.want)
   242  			}
   243  		})
   244  		if tt.args.env != nil {
   245  			for k := range tt.args.env {
   246  				os.Unsetenv(k)
   247  			}
   248  		}
   249  	}
   250  }
   251  
   252  func Test_subnetAPIKeyURL(t *testing.T) {
   253  	type args struct {
   254  		env map[string]string
   255  	}
   256  	tests := []struct {
   257  		name string
   258  		args args
   259  		want string
   260  	}{
   261  		{
   262  			name: "default",
   263  			args: args{
   264  				env: nil,
   265  			},
   266  			want: "https://subnet.min.io/api/auth/api-key",
   267  		},
   268  		{
   269  			name: "with env",
   270  			args: args{
   271  				env: map[string]string{
   272  					"CONSOLE_SUBNET_URL": "http://oorgle",
   273  				},
   274  			},
   275  			want: "http://oorgle/api/auth/api-key",
   276  		},
   277  	}
   278  	for _, tt := range tests {
   279  		if tt.args.env != nil {
   280  			for k, v := range tt.args.env {
   281  				os.Setenv(k, v)
   282  			}
   283  		}
   284  		t.Run(tt.name, func(_ *testing.T) {
   285  			if got := subnetAPIKeyURL(); got != tt.want {
   286  				t.Errorf("subnetAPIKeyURL() = %v, want %v", got, tt.want)
   287  			}
   288  		})
   289  		if tt.args.env != nil {
   290  			for k := range tt.args.env {
   291  				os.Unsetenv(k)
   292  			}
   293  		}
   294  	}
   295  }
   296  
   297  func TestLogWebhookURL(t *testing.T) {
   298  	type args struct {
   299  		env map[string]string
   300  	}
   301  	tests := []struct {
   302  		name string
   303  		args args
   304  		want string
   305  	}{
   306  		{
   307  			name: "default",
   308  			args: args{
   309  				env: nil,
   310  			},
   311  			want: "https://subnet.min.io/api/logs",
   312  		},
   313  		{
   314  			name: "with env",
   315  			args: args{
   316  				env: map[string]string{
   317  					"CONSOLE_SUBNET_URL": "http://oorgle",
   318  				},
   319  			},
   320  			want: "http://oorgle/api/logs",
   321  		},
   322  	}
   323  	for _, tt := range tests {
   324  		if tt.args.env != nil {
   325  			for k, v := range tt.args.env {
   326  				os.Setenv(k, v)
   327  			}
   328  		}
   329  		t.Run(tt.name, func(_ *testing.T) {
   330  			if got := LogWebhookURL(); got != tt.want {
   331  				t.Errorf("LogWebhookURL() = %v, want %v", got, tt.want)
   332  			}
   333  		})
   334  		if tt.args.env != nil {
   335  			for k := range tt.args.env {
   336  				os.Unsetenv(k)
   337  			}
   338  		}
   339  	}
   340  }
   341  
   342  func TestUploadURL(t *testing.T) {
   343  	type args struct {
   344  		env        map[string]string
   345  		uploadType string
   346  		filename   string
   347  	}
   348  	tests := []struct {
   349  		name string
   350  		args args
   351  		want string
   352  	}{
   353  		{
   354  			name: "default",
   355  			args: args{
   356  				env:        nil,
   357  				uploadType: "x",
   358  				filename:   "y.jpg",
   359  			},
   360  			want: "https://subnet.min.io/api/x/upload?filename=y.jpg",
   361  		},
   362  		{
   363  			name: "with env",
   364  			args: args{
   365  				env: map[string]string{
   366  					"CONSOLE_SUBNET_URL": "http://oorgle",
   367  				},
   368  				uploadType: "x",
   369  				filename:   "y.jpg",
   370  			},
   371  			want: "http://oorgle/api/x/upload?filename=y.jpg",
   372  		},
   373  	}
   374  	for _, tt := range tests {
   375  		if tt.args.env != nil {
   376  			for k, v := range tt.args.env {
   377  				os.Setenv(k, v)
   378  			}
   379  		}
   380  		t.Run(tt.name, func(_ *testing.T) {
   381  			if got := UploadURL(tt.args.uploadType, tt.args.filename); got != tt.want {
   382  				t.Errorf("UploadURL() = %v, want %v", got, tt.want)
   383  			}
   384  		})
   385  		if tt.args.env != nil {
   386  			for k := range tt.args.env {
   387  				os.Unsetenv(k)
   388  			}
   389  		}
   390  	}
   391  }
   392  
   393  func TestUploadAuthHeaders(t *testing.T) {
   394  	type args struct {
   395  		apiKey string
   396  	}
   397  	tests := []struct {
   398  		name string
   399  		args args
   400  		want map[string]string
   401  	}{
   402  		{
   403  			name: "basic",
   404  			args: args{
   405  				apiKey: "xx",
   406  			},
   407  			want: map[string]string{"x-subnet-api-key": "xx"},
   408  		},
   409  	}
   410  	for _, tt := range tests {
   411  		t.Run(tt.name, func(_ *testing.T) {
   412  			if got := UploadAuthHeaders(tt.args.apiKey); !reflect.DeepEqual(got, tt.want) {
   413  				t.Errorf("UploadAuthHeaders() = %v, want %v", got, tt.want)
   414  			}
   415  		})
   416  	}
   417  }
   418  
   419  func TestGenerateRegToken(t *testing.T) {
   420  	type args struct {
   421  		clusterRegInfo cmd.ClusterRegistrationInfo
   422  	}
   423  	tests := []struct {
   424  		name    string
   425  		args    args
   426  		want    string
   427  		wantErr bool
   428  	}{
   429  		{
   430  			name: "basic",
   431  			args: args{
   432  				clusterRegInfo: cmd.ClusterRegistrationInfo{
   433  					DeploymentID: "x",
   434  					ClusterName:  "y",
   435  					UsedCapacity: 1,
   436  					Info:         cmd.ClusterInfo{},
   437  				},
   438  			},
   439  			want:    "eyJkZXBsb3ltZW50X2lkIjoieCIsImNsdXN0ZXJfbmFtZSI6InkiLCJ1c2VkX2NhcGFjaXR5IjoxLCJpbmZvIjp7Im1pbmlvX3ZlcnNpb24iOiIiLCJub19vZl9zZXJ2ZXJfcG9vbHMiOjAsIm5vX29mX3NlcnZlcnMiOjAsIm5vX29mX2RyaXZlcyI6MCwibm9fb2ZfYnVja2V0cyI6MCwibm9fb2Zfb2JqZWN0cyI6MCwidG90YWxfZHJpdmVfc3BhY2UiOjAsInVzZWRfZHJpdmVfc3BhY2UiOjB9fQ==",
   440  			wantErr: false,
   441  		},
   442  	}
   443  	for _, tt := range tests {
   444  		t.Run(tt.name, func(_ *testing.T) {
   445  			got, err := GenerateRegToken(tt.args.clusterRegInfo)
   446  			if (err != nil) != tt.wantErr {
   447  				t.Errorf("GenerateRegToken() error = %v, wantErr %v", err, tt.wantErr)
   448  				return
   449  			}
   450  			if got != tt.want {
   451  				t.Errorf("GenerateRegToken() got = %v, want %v", got, tt.want)
   452  			}
   453  		})
   454  	}
   455  }
   456  
   457  func Test_subnetAuthHeaders(t *testing.T) {
   458  	type args struct {
   459  		authToken string
   460  	}
   461  	tests := []struct {
   462  		name string
   463  		args args
   464  		want map[string]string
   465  	}{
   466  		{
   467  			name: "basic",
   468  			args: args{
   469  				authToken: "x",
   470  			},
   471  			want: map[string]string{"Authorization": "Bearer x"},
   472  		},
   473  	}
   474  	for _, tt := range tests {
   475  		t.Run(tt.name, func(_ *testing.T) {
   476  			if got := subnetAuthHeaders(tt.args.authToken); !reflect.DeepEqual(got, tt.want) {
   477  				t.Errorf("subnetAuthHeaders() = %v, want %v", got, tt.want)
   478  			}
   479  		})
   480  	}
   481  }