added unit tests

This commit is contained in:
Souhaila Noor 2021-12-13 10:35:56 -06:00
parent e5052a864b
commit 9e2cf81127
2 changed files with 66 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import (
"log"
"net"
"net/url"
"path/filepath"
"runtime"
"strconv"
"strings"
@ -613,37 +614,51 @@ func (c *Config) ValidateAuth() ([]error, error) {
errors := make([]error, 0)
for name, value := range authConfig {
if value == "" {
errors = append(errors, fmt.Errorf("empty string for auth config %s", name))
errors = append(errors, fmt.Errorf("Empty string for auth config %s", name))
continue
}
if strings.Contains(name, "URL") {
_, err := url.ParseRequestURI(value)
if err != nil {
errors = append(errors, fmt.Errorf("invalid URL for auth config %s: %s", name, err))
errors = append(errors, fmt.Errorf("Invalid URL for auth config %s: %s", name, err))
continue
}
}
if strings.Contains(name, "File") {
yamlData := auth.ReadPermissionsFile(value)
var p auth.GroupPermissions
p.CreatePermissionsStruct(yamlData)
if len(p.Permissions) == 0 {
errors = append(errors, fmt.Errorf("No group permissions found in permissions file: %s", value))
fileExt := filepath.Ext(value)
if (fileExt != ".yaml") && (fileExt != ".yml") {
errors = append(errors, fmt.Errorf("Invalid file extension for auth config %s: %s", name, value))
continue
}
}
}
if len(errors) > 0 {
return errors, fmt.Errorf("there were errors validating config")
return errors, fmt.Errorf("There were errors validating config")
}
return errors, nil
}
func (c *Config) ValidatePermissions() (err error) {
yamlData := auth.ReadPermissionsFile(c.Auth.PermissionsFile)
var p auth.GroupPermissions
p.CreatePermissionsStruct(yamlData)
if len(p.Permissions) == 0 {
return fmt.Errorf("No group permissions found in permissions file: %s", c.Auth.PermissionsFile)
}
return nil
}
func (c *Config) MustValidateAuth() {
if errors, err := c.ValidateAuth(); err != nil {
for _, e := range errors {
log.Println(e)
for _, e1 := range errors {
log.Println(e1)
}
if e2 := c.ValidatePermissions(); e2 != nil {
log.Println(e2)
}
log.Fatal(err)
}

View file

@ -279,12 +279,15 @@ func TestConfig_validateAddrsGRPC(t *testing.T) {
}
func TestConfig_validateAuth(t *testing.T) {
errorMesgEmpty := "empty string"
errorMesgURL := "invalid URL"
errorMesgEmpty := "Empty string"
errorMesgURL := "Invalid URL"
errorMesgPermissions := "Invalid file extension"
validTestURL := "https://url.com/"
validClientID := "clientid"
validClientSecret := "clientSecret"
notValidURL := "not-a-url"
validFilename := "permissions.yaml"
invalidFilename := "permissions.txt"
invalidURL := "not-a-url"
emptyString := ""
enable := true
disable := false
@ -303,6 +306,7 @@ func TestConfig_validateAuth(t *testing.T) {
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
},
auth.Auth{
Enable: enable,
@ -312,6 +316,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: emptyString,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
{
@ -322,6 +327,7 @@ func TestConfig_validateAuth(t *testing.T) {
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
},
auth.Auth{
Enable: enable,
@ -331,6 +337,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: emptyString,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
{
@ -341,6 +348,7 @@ func TestConfig_validateAuth(t *testing.T) {
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
},
auth.Auth{
Enable: enable,
@ -350,6 +358,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: emptyString,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
{
@ -359,6 +368,7 @@ func TestConfig_validateAuth(t *testing.T) {
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
},
auth.Auth{
Enable: enable,
@ -368,6 +378,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: emptyString,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
{
@ -376,6 +387,7 @@ func TestConfig_validateAuth(t *testing.T) {
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
},
auth.Auth{
Enable: enable,
@ -385,6 +397,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: emptyString,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
{
@ -392,6 +405,7 @@ func TestConfig_validateAuth(t *testing.T) {
[]string{
errorMesgEmpty,
errorMesgEmpty,
errorMesgEmpty,
},
auth.Auth{
Enable: enable,
@ -401,6 +415,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: validTestURL,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
{
@ -412,10 +427,11 @@ func TestConfig_validateAuth(t *testing.T) {
Enable: enable,
ClientId: validClientID,
ClientSecret: validClientSecret,
AuthorizeURL: notValidURL,
AuthorizeURL: invalidURL,
TokenURL: validTestURL,
GroupEndpointURL: validTestURL,
ScopeURL: validTestURL,
PermissionsFile: validFilename,
},
},
{
@ -429,9 +445,26 @@ func TestConfig_validateAuth(t *testing.T) {
ClientId: validClientID,
ClientSecret: validClientSecret,
AuthorizeURL: validTestURL,
TokenURL: notValidURL,
GroupEndpointURL: notValidURL,
TokenURL: invalidURL,
GroupEndpointURL: invalidURL,
ScopeURL: validTestURL,
PermissionsFile: validFilename,
},
},
{
// Auth enabled, permissions file is set to invalid string
[]string{
errorMesgPermissions,
},
auth.Auth{
Enable: enable,
ClientId: validClientID,
ClientSecret: validClientSecret,
AuthorizeURL: validTestURL,
TokenURL: validTestURL,
GroupEndpointURL: validTestURL,
ScopeURL: validTestURL,
PermissionsFile: invalidFilename,
},
},
{
@ -445,6 +478,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: validTestURL,
GroupEndpointURL: validTestURL,
ScopeURL: validTestURL,
PermissionsFile: validFilename,
},
},
{
@ -458,6 +492,7 @@ func TestConfig_validateAuth(t *testing.T) {
TokenURL: emptyString,
GroupEndpointURL: emptyString,
ScopeURL: emptyString,
PermissionsFile: emptyString,
},
},
}