github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/config/credentials/file_store_test.go (about)

     1  package credentials
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/cli/cli/config/types"
     7  	"gotest.tools/assert"
     8  	is "gotest.tools/assert/cmp"
     9  )
    10  
    11  type fakeStore struct {
    12  	configs map[string]types.AuthConfig
    13  }
    14  
    15  func (f *fakeStore) Save() error {
    16  	return nil
    17  }
    18  
    19  func (f *fakeStore) GetAuthConfigs() map[string]types.AuthConfig {
    20  	return f.configs
    21  }
    22  
    23  func (f *fakeStore) GetFilename() string {
    24  	return "/tmp/docker-fakestore"
    25  }
    26  
    27  func newStore(auths map[string]types.AuthConfig) store {
    28  	return &fakeStore{configs: auths}
    29  }
    30  
    31  func TestFileStoreAddCredentials(t *testing.T) {
    32  	f := newStore(make(map[string]types.AuthConfig))
    33  
    34  	s := NewFileStore(f)
    35  	auth := types.AuthConfig{
    36  		Auth:          "super_secret_token",
    37  		Email:         "foo@example.com",
    38  		ServerAddress: "https://example.com",
    39  	}
    40  	err := s.Store(auth)
    41  	assert.NilError(t, err)
    42  	assert.Check(t, is.Len(f.GetAuthConfigs(), 1))
    43  
    44  	actual, ok := f.GetAuthConfigs()["https://example.com"]
    45  	assert.Check(t, ok)
    46  	assert.Check(t, is.DeepEqual(auth, actual))
    47  }
    48  
    49  func TestFileStoreGet(t *testing.T) {
    50  	f := newStore(map[string]types.AuthConfig{
    51  		"https://example.com": {
    52  			Auth:          "super_secret_token",
    53  			Email:         "foo@example.com",
    54  			ServerAddress: "https://example.com",
    55  		},
    56  	})
    57  
    58  	s := NewFileStore(f)
    59  	a, err := s.Get("https://example.com")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if a.Auth != "super_secret_token" {
    64  		t.Fatalf("expected auth `super_secret_token`, got %s", a.Auth)
    65  	}
    66  	if a.Email != "foo@example.com" {
    67  		t.Fatalf("expected email `foo@example.com`, got %s", a.Email)
    68  	}
    69  }
    70  
    71  func TestFileStoreGetAll(t *testing.T) {
    72  	s1 := "https://example.com"
    73  	s2 := "https://example2.com"
    74  	f := newStore(map[string]types.AuthConfig{
    75  		s1: {
    76  			Auth:          "super_secret_token",
    77  			Email:         "foo@example.com",
    78  			ServerAddress: "https://example.com",
    79  		},
    80  		s2: {
    81  			Auth:          "super_secret_token2",
    82  			Email:         "foo@example2.com",
    83  			ServerAddress: "https://example2.com",
    84  		},
    85  	})
    86  
    87  	s := NewFileStore(f)
    88  	as, err := s.GetAll()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	if len(as) != 2 {
    93  		t.Fatalf("wanted 2, got %d", len(as))
    94  	}
    95  	if as[s1].Auth != "super_secret_token" {
    96  		t.Fatalf("expected auth `super_secret_token`, got %s", as[s1].Auth)
    97  	}
    98  	if as[s1].Email != "foo@example.com" {
    99  		t.Fatalf("expected email `foo@example.com`, got %s", as[s1].Email)
   100  	}
   101  	if as[s2].Auth != "super_secret_token2" {
   102  		t.Fatalf("expected auth `super_secret_token2`, got %s", as[s2].Auth)
   103  	}
   104  	if as[s2].Email != "foo@example2.com" {
   105  		t.Fatalf("expected email `foo@example2.com`, got %s", as[s2].Email)
   106  	}
   107  }
   108  
   109  func TestFileStoreErase(t *testing.T) {
   110  	f := newStore(map[string]types.AuthConfig{
   111  		"https://example.com": {
   112  			Auth:          "super_secret_token",
   113  			Email:         "foo@example.com",
   114  			ServerAddress: "https://example.com",
   115  		},
   116  	})
   117  
   118  	s := NewFileStore(f)
   119  	err := s.Erase("https://example.com")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	// file store never returns errors, check that the auth config is empty
   125  	a, err := s.Get("https://example.com")
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	if a.Auth != "" {
   131  		t.Fatalf("expected empty auth token, got %s", a.Auth)
   132  	}
   133  	if a.Email != "" {
   134  		t.Fatalf("expected empty email, got %s", a.Email)
   135  	}
   136  }