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