summaryrefslogtreecommitdiff
path: root/pkg/auth/auth_test.go
blob: a0b97b10660e23f6d4d8f7f62c3cb4ad003d8b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package auth

import (
	"encoding/base64"
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/containers/image/v5/types"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestAuthConfigsToAuthFile(t *testing.T) {
	for _, tc := range []struct {
		name             string
		server           string
		shouldErr        bool
		expectedContains string
	}{
		{
			name:             "empty auth configs",
			server:           "",
			shouldErr:        false,
			expectedContains: "{}",
		},
		{
			name:             "registry with prefix",
			server:           "my-registry.local/username",
			shouldErr:        false,
			expectedContains: `"my-registry.local/username":`,
		},
		{
			name:             "normalize https:// prefix",
			server:           "http://my-registry.local/username",
			shouldErr:        false,
			expectedContains: `"my-registry.local/username":`,
		},
		{
			name:             "normalize docker registry with https prefix",
			server:           "http://index.docker.io/v1/",
			shouldErr:        false,
			expectedContains: `"index.docker.io":`,
		},
		{
			name:             "normalize docker registry without https prefix",
			server:           "docker.io/v2/",
			shouldErr:        false,
			expectedContains: `"docker.io":`,
		},
	} {
		configs := map[string]types.DockerAuthConfig{}
		if tc.server != "" {
			configs[tc.server] = types.DockerAuthConfig{}
		}

		filePath, err := authConfigsToAuthFile(configs)

		if tc.shouldErr {
			assert.NotNil(t, err)
			assert.Empty(t, filePath)
		} else {
			assert.Nil(t, err)
			content, err := ioutil.ReadFile(filePath)
			assert.Nil(t, err)
			assert.Contains(t, string(content), tc.expectedContains)
		}
	}
}

func TestParseSingleAuthHeader(t *testing.T) {
	for _, tc := range []struct {
		input     string
		shouldErr bool
		expected  map[string]types.DockerAuthConfig
	}{
		{
			input:    "", // An empty (or missing) header
			expected: map[string]types.DockerAuthConfig{"0": {}},
		},
		{
			input:    "null",
			expected: map[string]types.DockerAuthConfig{"0": {}},
		},
		// Invalid JSON
		{input: "@", shouldErr: true},
		// Success
		{
			input: base64.URLEncoding.EncodeToString([]byte(`{"username":"u1","password":"p1"}`)),
			expected: map[string]types.DockerAuthConfig{
				"0": {Username: "u1", Password: "p1"},
			},
		},
	} {
		req, err := http.NewRequest(http.MethodPost, "/", nil)
		require.NoError(t, err, tc.input)
		req.Header.Set(XRegistryAuthHeader.String(), tc.input)
		res, err := parseSingleAuthHeader(req)
		if tc.shouldErr {
			assert.Error(t, err, tc.input)
		} else {
			require.NoError(t, err, tc.input)
			assert.Equal(t, tc.expected, res, tc.input)
		}
	}
}

func TestParseMultiAuthHeader(t *testing.T) {
	for _, tc := range []struct {
		input     string
		shouldErr bool
		expected  map[string]types.DockerAuthConfig
	}{
		// Empty header
		{input: "", expected: nil},
		// "null"
		{input: "null", expected: nil},
		// Invalid JSON
		{input: "@", shouldErr: true},
		// Success
		{
			input: base64.URLEncoding.EncodeToString([]byte(
				`{"https://index.docker.io/v1/":{"username":"u1","password":"p1"},` +
					`"quay.io/libpod":{"username":"u2","password":"p2"}}`)),
			expected: map[string]types.DockerAuthConfig{
				"https://index.docker.io/v1/": {Username: "u1", Password: "p1"},
				"quay.io/libpod":              {Username: "u2", Password: "p2"},
			},
		},
	} {
		req, err := http.NewRequest(http.MethodPost, "/", nil)
		require.NoError(t, err, tc.input)
		req.Header.Set(XRegistryAuthHeader.String(), tc.input)
		res, err := parseMultiAuthHeader(req)
		if tc.shouldErr {
			assert.Error(t, err, tc.input)
		} else {
			require.NoError(t, err, tc.input)
			assert.Equal(t, tc.expected, res, tc.input)
		}
	}
}