harness-drone/plugin/registry/auths/auth_test.go

111 lines
2.3 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package auths
import (
"os"
"testing"
"github.com/drone/drone/core"
"github.com/google/go-cmp/cmp"
)
func TestParse(t *testing.T) {
got, err := ParseString(sample)
if err != nil {
t.Error(err)
return
}
want := []*core.Registry{
{
Address: "https://index.docker.io/v1/",
Username: "octocat",
Password: "correct-horse-battery-staple",
},
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}
func TestParseBytes(t *testing.T) {
got, err := ParseBytes([]byte(sample))
if err != nil {
t.Error(err)
return
}
want := []*core.Registry{
{
Address: "https://index.docker.io/v1/",
Username: "octocat",
Password: "correct-horse-battery-staple",
},
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}
func TestParseErr(t *testing.T) {
_, err := ParseString("")
if err == nil {
t.Errorf("Expect unmarshal error")
}
}
func TestParseFile(t *testing.T) {
got, err := ParseFile("./testdata/config.json")
if err != nil {
t.Error(err)
return
}
want := []*core.Registry{
{
Address: "https://index.docker.io/v1/",
Username: "octocat",
Password: "correct-horse-battery-staple",
},
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}
func TestParseFileErr(t *testing.T) {
_, err := ParseFile("./testdata/x.json")
if _, ok := err.(*os.PathError); !ok {
t.Errorf("Expect error when file does not exist")
}
}
func TestEncodeDecode(t *testing.T) {
username := "octocat"
password := "correct-horse-battery-staple"
encoded := encode(username, password)
decodedUsername, decodedPassword := decode(encoded)
if got, want := decodedUsername, username; got != want {
t.Errorf("Want decoded username %s, got %s", want, got)
}
if got, want := decodedPassword, password; got != want {
t.Errorf("Want decoded password %s, got %s", want, got)
}
}
func TestDecodeInvalid(t *testing.T) {
username, password := decode("b2N0b2NhdDp==")
if username != "" || password != "" {
t.Errorf("Expect decoding error")
}
}
var sample = `{
"auths": {
"https://index.docker.io/v1/": {
"auth": "b2N0b2NhdDpjb3JyZWN0LWhvcnNlLWJhdHRlcnktc3RhcGxl"
}
}
}`