github.com/microsoft/moc@v0.17.1/pkg/auth/auth_env_test.go (about)

     1  // Copyright (c) Microsoft Corporation.
     2  // Licensed under the Apache v2.0 license.
     3  package auth
     4  
     5  import (
     6  	"crypto/rand"
     7  	"crypto/rsa"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  var key *rsa.PrivateKey
    14  
    15  func init() {
    16  	os.MkdirAll("/tmp/auth", os.ModePerm)
    17  	key, _ = rsa.GenerateKey(rand.Reader, 2048)
    18  }
    19  
    20  func Test_GetWssdConfigLocationWssdConfigPathSet(t *testing.T) {
    21  
    22  	os.Unsetenv(AccessFileDirPath)
    23  	os.Setenv(WssdConfigPath, "TestWssdConfigPath")
    24  
    25  	wssdConfigPath := os.Getenv(WssdConfigPath)
    26  	path := GetWssdConfigLocation()
    27  	expectedPath := wssdConfigPath
    28  	if path != expectedPath {
    29  		t.Errorf("Invalid path when ACCESSFILE_DIR_PATH is not set and WSSD_CONFIG_PATH is set! Expected %s Actual %s", expectedPath, path)
    30  	}
    31  }
    32  
    33  func Test_GetWssdConfigLocationEnvNotSet(t *testing.T) {
    34  
    35  	os.Unsetenv(WssdConfigPath)
    36  	os.Unsetenv(AccessFileDirPath)
    37  
    38  	path := GetWssdConfigLocation()
    39  	wd, err := os.UserHomeDir()
    40  	if err != nil {
    41  		t.Errorf("Failed to get user home directory path %v", err)
    42  	}
    43  	execName, err := getExecutableName()
    44  	if err != nil {
    45  		t.Errorf("Failed to get executable name %v", err)
    46  	}
    47  	expectedPath := filepath.Join(wd, ".wssd", execName, "cloudconfig")
    48  	if path != expectedPath {
    49  		t.Errorf("Invalid path when both ACCESSFILE_DIR_PATH and WSSD_CONFIG_PATH env variables are not set! Expected %s Actual %s", expectedPath, path)
    50  	}
    51  }
    52  
    53  func Test_GetWssdConfigLocationAccessFileDirPathSet(t *testing.T) {
    54  
    55  	os.Setenv(AccessFileDirPath, "TestAccessFileDirPath")
    56  	accessFileDirPath := os.Getenv(AccessFileDirPath)
    57  	path := GetWssdConfigLocation()
    58  	execName, err := getExecutableName()
    59  	if err != nil {
    60  		t.Errorf("Failed to get executable name %v", err)
    61  	}
    62  	expectedPath := filepath.Join(accessFileDirPath, execName, "cloudconfig")
    63  	if path != expectedPath {
    64  		t.Errorf("Invalid path when ACCESSFILE_DIR_PATH env variable is set! Expected %s Actual %s", expectedPath, path)
    65  	}
    66  }
    67  
    68  func Test_GetWssdConfigLocationName(t *testing.T) {
    69  	path := GetMocConfigLocationName("", "")
    70  	wd, err := os.UserHomeDir()
    71  	if err != nil {
    72  		t.Errorf("Failed getting home path %v", err)
    73  	}
    74  	expectedPath := filepath.Join(wd, ".wssd/cloudconfig")
    75  	if path != expectedPath {
    76  		t.Errorf("Invalid path when not passed no subfolder or filename Expected %s Actual %s", expectedPath, path)
    77  	}
    78  }
    79  
    80  func Test_GetWssdConfigLocationNameWithSubfolder(t *testing.T) {
    81  	path := GetMocConfigLocationName("test", "")
    82  	wd, err := os.UserHomeDir()
    83  	if err != nil {
    84  		t.Errorf("Failed getting home path %v", err)
    85  	}
    86  	expectedPath := filepath.Join(wd, ".wssd/test/cloudconfig")
    87  	if path != expectedPath {
    88  		t.Errorf("Invalid path when not passed no subfolder or filename Expected %s Actual %s", expectedPath, path)
    89  	}
    90  }
    91  
    92  func Test_GetWssdConfigLocationNameWithSubfolderName(t *testing.T) {
    93  	path := GetMocConfigLocationName("test", "cc")
    94  	wd, err := os.UserHomeDir()
    95  	if err != nil {
    96  		t.Errorf("Failed getting home path %v", err)
    97  	}
    98  	expectedPath := filepath.Join(wd, ".wssd/test/cc")
    99  	if path != expectedPath {
   100  		t.Errorf("Invalid path when not passed no subfolder or filename Expected %s Actual %s", expectedPath, path)
   101  	}
   102  }
   103  
   104  func Test_GetWssdConfigLocationNameWithName(t *testing.T) {
   105  	path := GetMocConfigLocationName("", "cc")
   106  	wd, err := os.UserHomeDir()
   107  	if err != nil {
   108  		t.Errorf("Failed getting home path %v", err)
   109  	}
   110  	expectedPath := filepath.Join(wd, ".wssd/cc")
   111  	if path != expectedPath {
   112  		t.Errorf("Invalid path when not passed no subfolder or filename Expected %s Actual %s", expectedPath, path)
   113  	}
   114  }