-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from film42/gt/better_config
Gt/better config
- Loading branch information
Showing
7 changed files
with
221 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
) | ||
|
||
var InvalidCredentials = errors.New("Invalid credentials provided. Must have a username/ password or none at all.") | ||
|
||
type Credential struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
type Config struct { | ||
Credentials []Credential | ||
StripProxyHeaders bool `json:"strip_proxy_headers"` | ||
Port int | ||
} | ||
|
||
func (config *Config) AuthenticationRequired() bool { | ||
return len(config.Credentials) > 0 | ||
} | ||
|
||
func validCredentials(username, password string) bool { | ||
if username == "" && password == "" { | ||
return true | ||
} | ||
if username != "" && password != "" { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (config *Config) Validate() error { | ||
for _, credential := range config.Credentials { | ||
if !validCredentials(credential.Username, credential.Password) { | ||
return InvalidCredentials | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (config *Config) IsAuthenticated(username, password string) bool { | ||
for _, credential := range config.Credentials { | ||
if credential.Username == username && credential.Password == password { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func NewConfigFromReader(reader io.Reader) (*Config, error) { | ||
config := new(Config) | ||
decoder := json.NewDecoder(reader) | ||
err := decoder.Decode(&config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLoadingConfigFromFile(t *testing.T) { | ||
sampleConfigFile, _ := os.Open("test/config.json") | ||
defer sampleConfigFile.Close() | ||
|
||
sampleConfig, _ := NewConfigFromReader(sampleConfigFile) | ||
if err := sampleConfig.Validate(); err != nil { | ||
t.Fatal("Expected config to be valid but found: ", err) | ||
} | ||
|
||
if sampleConfig.Port != 26000 { | ||
t.Fatal("Expected port to be 26000 but found:", sampleConfig.Port) | ||
} | ||
|
||
if !sampleConfig.StripProxyHeaders { | ||
t.Fatal("Expected sampleConfig.StripProxyHeaders to be true") | ||
} | ||
|
||
if !sampleConfig.AuthenticationRequired() { | ||
t.Fatal("Expected sample config to require authentication") | ||
} | ||
|
||
credentialsLen := len(sampleConfig.Credentials) | ||
if credentialsLen != 2 { | ||
t.Fatal("Expected config to have 2 credentials but found:", credentialsLen) | ||
} | ||
|
||
credential := Credential{Username: "login", Password: "yolo"} | ||
if sampleConfig.Credentials[0] != credential { | ||
t.Fatal("Expected", credential, "but found", sampleConfig.Credentials[0]) | ||
} | ||
|
||
credential = Credential{Username: "ron.swanson", Password: "g0ld5topsTheGovt"} | ||
if sampleConfig.Credentials[1] != credential { | ||
t.Fatal("Expected", credential, "but found", sampleConfig.Credentials[1]) | ||
} | ||
} | ||
|
||
func TestInvalidConfigFormat(t *testing.T) { | ||
invalidFormatReader := strings.NewReader(`{"testing":"ok...`) | ||
_, err := NewConfigFromReader(invalidFormatReader) | ||
if err == nil { | ||
t.Fatal("Config should have returned an error for being invalid json") | ||
} | ||
} | ||
|
||
func TestConfigInavlidUsername(t *testing.T) { | ||
sampleConfig := &Config{ | ||
Credentials: []Credential{Credential{Username: "", Password: "test"}}, | ||
} | ||
|
||
if sampleConfig.Validate() != InvalidCredentials { | ||
t.Fatal("Expected username to be invalid") | ||
} | ||
} | ||
|
||
func TestConfigInavlidPassword(t *testing.T) { | ||
sampleConfig := &Config{ | ||
Credentials: []Credential{Credential{Username: "test", Password: ""}}, | ||
} | ||
|
||
if sampleConfig.Validate() != InvalidCredentials { | ||
t.Fatal("Expected password to be invalid") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"port": 26000, | ||
"strip_proxy_headers": true, | ||
"credentials": [ | ||
{ | ||
"username": "login", | ||
"password": "yolo" | ||
}, | ||
{ | ||
"username": "ron.swanson", | ||
"password": "g0ld5topsTheGovt" | ||
} | ||
] | ||
} |