...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/emulatorsvc/config_test.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/emulatorsvc

     1  package emulatorsvc
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  // authFile tests
    12  
    13  func TestOpenAuthFile(t *testing.T) {
    14  	t.Parallel()
    15  	dir := t.TempDir()
    16  	emptyDir := t.TempDir()
    17  
    18  	cases := map[string]struct {
    19  		dir       string
    20  		data      []byte
    21  		expected  *AuthFile
    22  		errAssert assert.ErrorAssertionFunc
    23  	}{
    24  		"Valid": {
    25  			dir: dir,
    26  			data: []byte(`
    27  defaultProfile: default
    28  profiles:
    29    default:
    30      username: a
    31      organization: b
    32      apiEndpoint: c
    33    second:
    34      username: d
    35      organization: e
    36      apiEndpoint: f
    37    third:
    38      username: g
    39      organization: h
    40      apiEndpoint: i
    41  `),
    42  			expected: &AuthFile{
    43  				path:           filepath.Join(dir, authFileName),
    44  				DefaultProfile: "default",
    45  				Profiles: map[string]Profile{
    46  					"default": {
    47  						Username:     "a",
    48  						Organization: "b",
    49  						API:          "c",
    50  					},
    51  					"second": {
    52  						Username:     "d",
    53  						Organization: "e",
    54  						API:          "f",
    55  					},
    56  					"third": {
    57  						Username:     "g",
    58  						Organization: "h",
    59  						API:          "i",
    60  					},
    61  				},
    62  			},
    63  			errAssert: assert.NoError,
    64  		},
    65  		"Invalid File": {
    66  			dir: emptyDir,
    67  			data: []byte(`
    68  defau
    69  `),
    70  			expected:  nil,
    71  			errAssert: assert.Error,
    72  		},
    73  		"Duplicate Profile Names": {
    74  			dir: dir,
    75  			data: []byte(`
    76  defaultProfile: default
    77  profiles:
    78    default:
    79      username: a
    80      organization: b
    81      apiEndpoint: c
    82    second:
    83      username: d
    84      organization: e
    85      apiEndpoint: f
    86    second:
    87      username: g
    88      organization: h
    89      apiEndpoint: i
    90  `),
    91  			expected:  nil,
    92  			errAssert: assert.Error,
    93  		},
    94  	}
    95  
    96  	for name, tc := range cases {
    97  		t.Run(name, func(t *testing.T) {
    98  			path := filepath.Join(tc.dir, authFileName)
    99  			err := os.WriteFile(path, tc.data, 0640)
   100  			assert.NoError(t, err)
   101  
   102  			af, err := OpenAuthFile(tc.dir)
   103  			tc.errAssert(t, err)
   104  			assert.Equal(t, tc.expected, af)
   105  		})
   106  	}
   107  }
   108  
   109  func TestOpenAuthFileFileDoesNotExist(t *testing.T) {
   110  	dir := t.TempDir()
   111  	expected := &AuthFile{
   112  		path:           filepath.Join(dir, authFileName),
   113  		DefaultProfile: "",
   114  		Profiles:       make(map[string]Profile),
   115  	}
   116  	actual, err := OpenAuthFile(dir)
   117  	assert.NoError(t, err)
   118  	assert.Equal(t, expected, actual)
   119  }
   120  
   121  func TestUpdateAuthFile(t *testing.T) {
   122  	t.Parallel()
   123  
   124  	cases := map[string]struct {
   125  		data      []byte
   126  		profile   Profile
   127  		name      string
   128  		expected  string
   129  		errAssert assert.ErrorAssertionFunc
   130  	}{
   131  		"Empty File No Name": {
   132  			data: nil,
   133  			profile: Profile{
   134  				Username:     "a",
   135  				Password:     "password",
   136  				Organization: "b",
   137  				API:          "c",
   138  			},
   139  			expected: `
   140  defaultProfile: default
   141  profiles:
   142    default:
   143      username: a
   144      organization: b
   145      apiEndpoint: c
   146  `,
   147  			errAssert: assert.NoError,
   148  		},
   149  		"Empty File Name Given": {
   150  			data: nil,
   151  			profile: Profile{
   152  				Username:     "a",
   153  				Password:     "password",
   154  				Organization: "b",
   155  				API:          "c",
   156  			},
   157  			name: "myProfile",
   158  			expected: `
   159  defaultProfile: myProfile
   160  profiles:
   161    myProfile:
   162      username: a
   163      organization: b
   164      apiEndpoint: c
   165  `,
   166  			errAssert: assert.NoError,
   167  		},
   168  		"No Default Profile": {
   169  			data: []byte(`
   170  profiles:
   171    profilename:
   172      username: a
   173      organization: b
   174      apiEndpoint: c
   175  `),
   176  			profile: Profile{
   177  				Username:     "a",
   178  				Password:     "password",
   179  				Organization: "b",
   180  				API:          "c",
   181  			},
   182  			expected: `
   183  defaultProfile: default
   184  profiles:
   185    profilename:
   186      username: a
   187      organization: b
   188      apiEndpoint: c
   189  `,
   190  			errAssert: assert.NoError,
   191  		},
   192  		"No Profiles": {
   193  			data: []byte(`
   194  defaultProfile: profilename
   195  `),
   196  			profile: Profile{
   197  				Username:     "a",
   198  				Password:     "password",
   199  				Organization: "b",
   200  				API:          "c",
   201  			},
   202  			expected: `
   203  defaultProfile: profilename
   204  profiles:
   205    profilename:
   206      username: a
   207      organization: b
   208      apiEndpoint: c
   209  `,
   210  			errAssert: assert.NoError,
   211  		},
   212  		"No Change With Identical Profile": {
   213  			data: []byte(`
   214  defaultProfile: default
   215  profiles:
   216    default:
   217      username: a
   218      organization: b
   219      apiEndpoint: c
   220    second:
   221      username: d
   222      organization: e
   223      apiEndpoint: f
   224  `),
   225  			profile: Profile{
   226  				Username:     "d",
   227  				Password:     "password",
   228  				Organization: "e",
   229  				API:          "f",
   230  			},
   231  			expected: `
   232  defaultProfile: default
   233  profiles:
   234    default:
   235      username: a
   236      organization: b
   237      apiEndpoint: c
   238    second:
   239      username: d
   240      organization: e
   241      apiEndpoint: f
   242  `,
   243  			errAssert: assert.NoError,
   244  		},
   245  		"No Change With Different Profile": {
   246  			data: []byte(`
   247  defaultProfile: default
   248  profiles:
   249    default:
   250      username: a
   251      organization: b
   252      apiEndpoint: c
   253    second:
   254      username: d
   255      organization: e
   256      apiEndpoint: f
   257  `),
   258  			profile: Profile{
   259  				Username:     "somethingelse",
   260  				Password:     "password",
   261  				Organization: "somethingelse",
   262  				API:          "somethingelse",
   263  			},
   264  			expected: `
   265  defaultProfile: default
   266  profiles:
   267    default:
   268      username: a
   269      organization: b
   270      apiEndpoint: c
   271    second:
   272      username: d
   273      organization: e
   274      apiEndpoint: f
   275  `,
   276  			errAssert: assert.NoError,
   277  		},
   278  	}
   279  
   280  	for name, tc := range cases {
   281  		t.Run(name, func(t *testing.T) {
   282  			dir := t.TempDir()
   283  			path := filepath.Join(dir, authFileName)
   284  			err := os.WriteFile(path, tc.data, 0640)
   285  			assert.NoError(t, err)
   286  
   287  			af, err := OpenAuthFile(dir)
   288  			assert.NoError(t, err)
   289  
   290  			err = af.UpdateAuthFile(tc.profile, tc.name)
   291  			tc.errAssert(t, err)
   292  
   293  			actual, err := os.ReadFile(path)
   294  			assert.NoError(t, err)
   295  			assert.YAMLEq(t, tc.expected, string(actual))
   296  		})
   297  	}
   298  }
   299  
   300  func TestWriteFile(t *testing.T) {
   301  	t.Parallel()
   302  
   303  	dir := t.TempDir()
   304  	path := filepath.Join(dir, authFileName)
   305  	af := AuthFile{
   306  		path:           path,
   307  		DefaultProfile: "default",
   308  		Profiles: map[string]Profile{
   309  			"default": {
   310  				Username:     "a",
   311  				Organization: "b",
   312  				API:          "c",
   313  			},
   314  		},
   315  	}
   316  	err := af.writeFile()
   317  	assert.NoError(t, err)
   318  
   319  	expected := `
   320  defaultProfile: default
   321  profiles:
   322    default:
   323      username: a
   324      organization: b
   325      apiEndpoint: c
   326  `
   327  	data, err := os.ReadFile(path)
   328  	assert.NoError(t, err)
   329  	assert.YAMLEq(t, expected, string(data))
   330  }
   331  
   332  func TestLoadProfile(t *testing.T) {
   333  	t.Parallel()
   334  
   335  	af := AuthFile{
   336  		DefaultProfile: "default",
   337  		Profiles: map[string]Profile{
   338  			"default": {
   339  				Username:     "a",
   340  				Organization: "b",
   341  				API:          "c",
   342  			},
   343  		},
   344  	}
   345  
   346  	cases := map[string]struct {
   347  		af        AuthFile
   348  		profile   Profile
   349  		name      string
   350  		expected  Profile
   351  		errAssert assert.ErrorAssertionFunc
   352  	}{
   353  		"Empty Profile": {
   354  			af:      af,
   355  			profile: Profile{},
   356  			name:    "default",
   357  			expected: Profile{
   358  				Username:     "a",
   359  				Organization: "b",
   360  				API:          "c",
   361  			},
   362  			errAssert: assert.NoError,
   363  		},
   364  		"Filled Profile Same Values": {
   365  			af: af,
   366  			profile: Profile{
   367  				Username:     "a",
   368  				Organization: "b",
   369  				API:          "c",
   370  			},
   371  			name: "default",
   372  			expected: Profile{
   373  				Username:     "a",
   374  				Organization: "b",
   375  				API:          "c",
   376  			},
   377  			errAssert: assert.NoError,
   378  		},
   379  		"Filled Profile Different Values": {
   380  			af: af,
   381  			profile: Profile{
   382  				Username: "somethingelse",
   383  				API:      "somethingelse",
   384  			},
   385  			name: "default",
   386  			expected: Profile{
   387  				Username:     "somethingelse",
   388  				Organization: "b",
   389  				API:          "somethingelse",
   390  			},
   391  			errAssert: assert.NoError,
   392  		},
   393  		"Name Not In Profiles": {
   394  			af: af,
   395  			profile: Profile{
   396  				Username:     "a",
   397  				Organization: "b",
   398  				API:          "c",
   399  			},
   400  			name:      "somethingelse",
   401  			expected:  Profile{},
   402  			errAssert: assert.Error,
   403  		},
   404  		"Unitialized Auth File": {
   405  			af: AuthFile{},
   406  			profile: Profile{
   407  				Username:     "a",
   408  				Organization: "b",
   409  				API:          "c",
   410  			},
   411  			name: "default",
   412  			expected: Profile{
   413  				Username:     "a",
   414  				Organization: "b",
   415  				API:          "c",
   416  			},
   417  			errAssert: assert.NoError,
   418  		},
   419  		"Empty Name": {
   420  			af: af,
   421  			profile: Profile{
   422  				Username:     "a",
   423  				Organization: "b",
   424  				API:          "c",
   425  			},
   426  			expected: Profile{
   427  				Username:     "a",
   428  				Organization: "b",
   429  				API:          "c",
   430  			},
   431  			errAssert: assert.NoError,
   432  		},
   433  	}
   434  
   435  	for name, tc := range cases {
   436  		tc := tc
   437  		t.Run(name, func(t *testing.T) {
   438  			t.Parallel()
   439  
   440  			actual, err := tc.af.LoadProfile(tc.profile, tc.name)
   441  			tc.errAssert(t, err)
   442  			assert.Equal(t, tc.expected, actual)
   443  		})
   444  	}
   445  }
   446  
   447  // Config tests
   448  
   449  func TestNewConfig(t *testing.T) {
   450  	dir := t.TempDir()
   451  	profile := Profile{"a", "b", "c", "d", ""}
   452  
   453  	expected := Config{dir, profile}
   454  	actual := NewConfig(dir, profile)
   455  	assert.Equal(t, expected, actual)
   456  }
   457  
   458  // Profile tests
   459  
   460  func TestRequestEmptyFieldsValid(t *testing.T) {
   461  	t.Parallel()
   462  
   463  	profile := Profile{
   464  		Username:     "a",
   465  		Password:     "b",
   466  		API:          "c",
   467  		Organization: "d",
   468  	}
   469  	assert.NoError(t, profile.RequestEmptyFields())
   470  }
   471  

View as plain text