-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcredentials_test.go
155 lines (131 loc) · 3.81 KB
/
credentials_test.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
type cleanup func()
func writeToTempFile(prefix string, name string, content string) (string, cleanup, error) {
tempdir, dirErr := ioutil.TempDir("", "cred_test")
if dirErr != nil {
return "", func() {}, dirErr
}
clean := func() { os.RemoveAll(tempdir) }
fullName := filepath.Join(tempdir, name)
if writeErr := ioutil.WriteFile(fullName, []byte(content), 0777); writeErr != nil {
return "", clean, writeErr
}
return fullName, clean, nil
}
func TestGetCredentialsFromIniFile(t *testing.T) {
iniConfig := `
user=iniUser
password=iniPassword
`
config, clean, cfgErr := writeToTempFile("inifile", "example.com", iniConfig)
defer clean()
if cfgErr != nil {
t.Errorf("Error on credential file write: %v", cfgErr)
}
creds, err := getCredentials(config)
if err != nil {
t.Errorf("Error on credential file read: %v", err)
t.FailNow()
}
if creds.User != "iniUser" || creds.Password != "iniPassword" {
t.Errorf("Credentials were not read correctly: %v", creds)
}
}
func TestGetCredentialsFromJsonFile(t *testing.T) {
jsonConfig := `
{
"host": "https://example.com",
"user": "jsonUser",
"password": "jsonPassword"
}
`
config, clean, cfgErr := writeToTempFile("jsonfile", "credentials.json", jsonConfig)
defer clean()
if cfgErr != nil {
t.Errorf("Error on credential file write: %v", cfgErr)
}
creds, err := getCredentials(config)
if err != nil {
t.Errorf("Error on credential file read: %v", err)
t.FailNow()
}
if creds.User != "jsonUser" || creds.Password != "jsonPassword" {
t.Errorf("Credentials were not read correctly: %v", creds)
}
}
func TestGetCredentialsFromEnvironment(t *testing.T) {
os.Setenv("ARTIFACTORY_USER", "envUser")
defer os.Setenv("ARTIFACTORY_USER", "")
os.Setenv("ARTIFACTORY_PASSWORD", "envPassword")
defer os.Setenv("ARTIFACTORY_PASSWORD", "")
creds, err := getCredentials("")
if err != nil {
t.Errorf("Error on credential environment read: %v", err)
t.FailNow()
}
if creds.User != "envUser" || creds.Password != "envPassword" {
t.Errorf("Environment was not read correctly: %v", creds)
}
}
func TestCredentialsIniFileIsVerified(t *testing.T) {
iniConfig := `
password=bar
`
config, clean, cfgErr := writeToTempFile("inimissing", "mising.com", iniConfig)
defer clean()
if cfgErr != nil {
t.Errorf("Error on credential file write: %v", cfgErr)
}
_, err := getCredentials(config)
if err == nil {
t.Errorf("Credentials file read is not verified")
}
}
func TestCredentialsJsonFileIsVerified(t *testing.T) {
jsonConfig := `
{
"host": "https://example.com",
"password": "jsonPassword"
}
`
config, clean, cfgErr := writeToTempFile("missing", "credentials.json", jsonConfig)
defer clean()
if cfgErr != nil {
t.Errorf("Error on credential file write: %v", cfgErr)
}
_, err := getCredentials(config)
if err == nil {
t.Errorf("Credentials file read is not verified")
}
}
func TestCredentialsNullWhenEnvironmentIsNotFullySet(t *testing.T) {
os.Setenv("ARTIFACTORY_USER", "envUser")
defer os.Setenv("ARTIFACTORY_USER", "")
creds, err := getCredentials("")
if err != nil || creds != nil {
t.Errorf("%v:%v", err, creds)
}
}
func TestMissingItemsInCredentials(t *testing.T) {
missingUser := &credentials{Password: "foo"}
if err := verifyCredentials(*missingUser, "bar"); err == nil {
t.Errorf("Missing user allowed")
}
missingPassword := &credentials{User: "foo"}
if err := verifyCredentials(*missingPassword, "boo"); err == nil {
t.Errorf("Missing password allowed")
}
good := &credentials{User: "foo", Password: "bar"}
if err := verifyCredentials(*good, "goo"); err != nil {
t.Errorf("error:%v", err)
}
}