github.com/drone/runner-go@v1.12.0/registry/auths/auth_test.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package auths 6 7 import ( 8 "bytes" 9 "encoding/base64" 10 "os" 11 "testing" 12 13 "github.com/drone/drone-go/drone" 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestParse(t *testing.T) { 18 got, err := ParseString(sample) 19 if err != nil { 20 t.Error(err) 21 return 22 } 23 want := []*drone.Registry{ 24 { 25 Address: "index.docker.io", 26 Username: "octocat", 27 Password: "correct-horse-battery-staple", 28 }, 29 } 30 if diff := cmp.Diff(got, want); diff != "" { 31 t.Errorf(diff) 32 } 33 } 34 35 func TestParseBytes(t *testing.T) { 36 got, err := ParseBytes([]byte(sample)) 37 if err != nil { 38 t.Error(err) 39 return 40 } 41 want := []*drone.Registry{ 42 { 43 Address: "index.docker.io", 44 Username: "octocat", 45 Password: "correct-horse-battery-staple", 46 }, 47 } 48 if diff := cmp.Diff(got, want); diff != "" { 49 t.Errorf(diff) 50 } 51 } 52 53 func TestParseErr(t *testing.T) { 54 _, err := ParseString("") 55 if err == nil { 56 t.Errorf("Expect unmarshal error") 57 } 58 } 59 60 func TestParseFile(t *testing.T) { 61 got, err := ParseFile("./testdata/config.json") 62 if err != nil { 63 t.Error(err) 64 return 65 } 66 want := []*drone.Registry{ 67 { 68 Address: "index.docker.io", 69 Username: "octocat", 70 Password: "correct-horse-battery-staple", 71 }, 72 } 73 if diff := cmp.Diff(got, want); diff != "" { 74 t.Errorf(diff) 75 } 76 } 77 78 func TestParseUsernamePassword(t *testing.T) { 79 got, err := ParseString(sample2) 80 if err != nil { 81 t.Error(err) 82 return 83 } 84 want := []*drone.Registry{ 85 { 86 Address: "index.docker.io", 87 Username: "octocat", 88 Password: "correct-horse-battery-staple", 89 }, 90 } 91 if diff := cmp.Diff(got, want); diff != "" { 92 t.Errorf(diff) 93 } 94 } 95 96 func TestParseFileErr(t *testing.T) { 97 _, err := ParseFile("./testdata/x.json") 98 if _, ok := err.(*os.PathError); !ok { 99 t.Errorf("Expect error when file does not exist") 100 } 101 } 102 103 func TestEncodeDecode(t *testing.T) { 104 username := "octocat" 105 password := "correct-horse-battery-staple" 106 107 encoded := encode(username, password) 108 decodedUsername, decodedPassword := decode(encoded) 109 if got, want := decodedUsername, username; got != want { 110 t.Errorf("Want decoded username %s, got %s", want, got) 111 } 112 if got, want := decodedPassword, password; got != want { 113 t.Errorf("Want decoded password %s, got %s", want, got) 114 } 115 } 116 117 func TestDecodeInvalid(t *testing.T) { 118 username, password := decode("b2N0b2NhdDp==") 119 if username != "" || password != "" { 120 t.Errorf("Expect decoding error") 121 } 122 } 123 124 func TestHeader(t *testing.T) { 125 username := "octocat" 126 password := "correct-horse-battery-staple" 127 result := Header(username, password) 128 got, err := base64.URLEncoding.DecodeString(result) 129 if err != nil { 130 t.Error(err) 131 return 132 } 133 want := []byte(`{"username":"octocat","password":"correct-horse-battery-staple"}`) 134 if bytes.Equal(got, want) == false { 135 t.Errorf("Could not decode credential header") 136 } 137 } 138 139 var sample = `{ 140 "auths": { 141 "https://index.docker.io/v1/": { 142 "auth": "b2N0b2NhdDpjb3JyZWN0LWhvcnNlLWJhdHRlcnktc3RhcGxl" 143 } 144 } 145 }` 146 147 var sample2 = `{ 148 "auths": { 149 "https://index.docker.io/v1/": { 150 "username": "octocat", 151 "password": "correct-horse-battery-staple" 152 } 153 } 154 }`