github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/cliconfig/credentials/file_store_test.go (about)

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