-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
missing test files on previous commit
- Loading branch information
Showing
10 changed files
with
224 additions
and
0 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
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()) | ||
} | ||
} |
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,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()) | ||
} | ||
} |
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,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()) | ||
} | ||
} |
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 @@ | ||
{"clientId":"","clientName":"[email protected]","clientSecret":"6ae15bff-1435-430f-975b-9b3d9914b698"} |
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 @@ | ||
{"clientId":"deeb55bd-2a55-4988-a345-d8fdddd0e0c9","clientName":"","clientSecret":"6ae15bff-1435-430f-975b-9b3d9914b698"} |
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 @@ | ||
{"clientId":"deeb55bd-2a55-4988-a345-d8fdddd0e0c9","clientName":"[email protected]","clientSecret":""} |
Empty file.
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 @@ | ||
{"clientId":"","clientName":"","clientSecret":""} |
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 @@ | ||
thisisinvalid |
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 @@ | ||
AstraCS:thisisavalidtoken |