Skip to content

Commit

Permalink
missing test files on previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Svihla authored and foundev committed Mar 29, 2021
1 parent 7bbd8bc commit 428e8b8
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pkg/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg

import (
"errors"
"fmt"
"testing"
)

func TestParseErrorNoArgs(t *testing.T) {
parseError := ParseError{
Err: errors.New("bogus error"),
}

expected := "no args provided"
if parseError.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, parseError.Error())
}
}

func TestParseError(t *testing.T) {
parseError := ParseError{
Args: []string{"a", "b"},
Err: errors.New("bogus error"),
}

expected := "Unable to parse command line with args: a, b. Nested error was 'bogus error'"
if parseError.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, parseError.Error())
}
}

func TestFileNotFoundError(t *testing.T) {
fileErr := FileNotFoundError{
Path: "/a/b/C",
Err: fmt.Errorf("Bogus Error"),
}
expected := "Unable to find file '/a/b/C' with error: 'Bogus Error'"
if fileErr.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, fileErr.Error())
}
}

func TestJSONParseError(t *testing.T) {
fileErr := JSONParseError{
Original: "invalid string",
Err: fmt.Errorf("Bogus Error"),
}
expected := "JSON parsing error for json 'invalid string' with error 'Bogus Error'"
if fileErr.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, fileErr.Error())
}
}
107 changes: 107 additions & 0 deletions pkg/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg

import (
"fmt"
"path"
"testing"
)

func TestUnableToReadHomeDir(t *testing.T) {
noPath := func() (string, error) { return "", fmt.Errorf("unexpected error") }
creds := &Creds{
GetHomeFunc: noPath,
}
_, err := creds.Login()
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "unable to read conf dir with error 'unable to get user home directory with error 'unexpected error''"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}
func TestMissingConfigFolder(t *testing.T) {
noPath := func() (string, error) { return "", nil }
creds := &Creds{
GetHomeFunc: noPath,
}
_, err := creds.Login()
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "unable to access any file for directory `.config/astra`, run astra-cli login first"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}

func TestLoginWithInvalidTokenFile(t *testing.T) {
invalid := func() (string, error) { return path.Join("testdata", "with_invalid_token"), nil }
creds := &Creds{
GetHomeFunc: invalid,
}
_, err := creds.Login()
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "found token at 'testdata/with_invalid_token/.config/astra/token' but unable to read token with error 'missing prefix 'AstraCS' in token file 'testdata/with_invalid_token/.config/astra/token''"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}

func TestLoginWithEmptyTokenFile(t *testing.T) {
invalid := func() (string, error) { return path.Join("testdata", "with_empty_token"), nil }
creds := &Creds{
GetHomeFunc: invalid,
}
_, err := creds.Login()
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "found token at 'testdata/with_empty_token/.config/astra/token' but unable to read token with error 'token file 'testdata/with_empty_token/.config/astra/token' is emtpy'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}

func TestLoginValidToken(t *testing.T) {
valid := func() (string, error) { return path.Join("testdata", "with_token"), nil }
creds := &Creds{
GetHomeFunc: valid,
}
_, err := creds.Login()
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
}

func TestLoginWithInvalidSA(t *testing.T) {
invalid := func() (string, error) { return path.Join("testdata", "with_invalid_sa"), nil }
creds := &Creds{
GetHomeFunc: invalid,
}
_, err := creds.Login()
if err == nil {
t.Fatal("expected an error on an empty path")
}
expected := "Invalid service account: Client ID for service account is emtpy for file 'testdata/with_invalid_sa/.config/astra/sa.json'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}
44 changes: 44 additions & 0 deletions pkg/strfmt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg

import (
"bytes"
"strings"
"testing"
)

func TestTabWriterLayout(t *testing.T) {
w := bytes.NewBufferString("")
rows := [][]string{
{
"abc", "def", "ghi",
},
{
"", "123456", "1",
},
}
err := WriteRows(w, rows)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
expected1 := "abc def ghi"
expected2 := " 123456 1"
expected := strings.Join([]string{expected1, expected2}, "\n")
if w.String() != expected {
t.Errorf("expected/actual \n'%v'\n'%v'", expected, w.String())
}
}
1 change: 1 addition & 0 deletions pkg/testdata/missing-id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientId":"","clientName":"[email protected]","clientSecret":"6ae15bff-1435-430f-975b-9b3d9914b698"}
1 change: 1 addition & 0 deletions pkg/testdata/missing-name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientId":"deeb55bd-2a55-4988-a345-d8fdddd0e0c9","clientName":"","clientSecret":"6ae15bff-1435-430f-975b-9b3d9914b698"}
1 change: 1 addition & 0 deletions pkg/testdata/missing-secret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientId":"deeb55bd-2a55-4988-a345-d8fdddd0e0c9","clientName":"[email protected]","clientSecret":""}
Empty file.
1 change: 1 addition & 0 deletions pkg/testdata/with_invalid_sa/.config/astra/sa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientId":"","clientName":"","clientSecret":""}
1 change: 1 addition & 0 deletions pkg/testdata/with_invalid_token/.config/astra/token
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
thisisinvalid
1 change: 1 addition & 0 deletions pkg/testdata/with_token/.config/astra/token
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AstraCS:thisisavalidtoken

0 comments on commit 428e8b8

Please sign in to comment.