-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(cli-client): add tests for client, validator and tokens
- Loading branch information
1 parent
dcedd24
commit edf6f07
Showing
6 changed files
with
212 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,29 @@ | ||
package cli_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/canonical/oci-factory/cli-client/internals/cli" | ||
) | ||
|
||
func TestValidateAndFormatDateLegalInput(t *testing.T) { | ||
inputs := []string{"2006-07-08", "2008-09-10", "2024-05-01"} | ||
|
||
for _, datetime := range inputs { | ||
_, err := cli.ValidateAndFormatDate(datetime) | ||
if err != nil { | ||
t.Fatalf("Failed parsing legal time format yyyy-mm-dd, %v", err) | ||
} | ||
} | ||
} | ||
|
||
func TestValidateAndFormatDateBadInput(t *testing.T) { | ||
inputs := []string{"2001-02-29", "2024-05-32", "01-01-2023"} | ||
|
||
for _, datetime := range inputs { | ||
_, err := cli.ValidateAndFormatDate(datetime) | ||
if err == nil { | ||
t.Fatalf("Parsing illegal date input [%s] didn't raise error", datetime) | ||
} | ||
} | ||
} |
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,46 @@ | ||
package client_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/canonical/oci-factory/cli-client/internals/client" | ||
"github.com/canonical/oci-factory/cli-client/internals/token" | ||
) | ||
|
||
func TestSendRequest(t *testing.T) { | ||
mockPayload := []byte(`{"mock":"payload"}`) | ||
expectedStatusCode := http.StatusOK | ||
|
||
// Create a mock server | ||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Verify the request method, URL, and payload | ||
if r.Method != http.MethodPost { | ||
t.Errorf("unexpected request method, want POST, got %s", r.Method) | ||
} | ||
body, _ := io.ReadAll(r.Body) | ||
if !bytes.Equal(body, mockPayload) { | ||
t.Errorf("unexpected request payload, want %s, got %s", string(mockPayload), string(body)) | ||
} | ||
// Set the response status code and body | ||
w.WriteHeader(expectedStatusCode) | ||
w.Write([]byte(`{"mock":"response"}`)) | ||
})) | ||
defer mockServer.Close() | ||
|
||
saveToken := os.Getenv(token.TokenVarName) | ||
os.Setenv(token.TokenVarName, "ghp_AAAAAAAA") | ||
// Call the SendRequest function | ||
response := client.SendRequest(http.MethodPost, mockServer.URL, mockPayload, expectedStatusCode) | ||
token.RestoreTokenEnv(saveToken) | ||
|
||
// Verify the response | ||
expectedResponse := []byte(`{"mock":"response"}`) | ||
if !bytes.Equal(response, expectedResponse) { | ||
t.Errorf("unexpected response, want %s, got %s", string(expectedResponse), string(response)) | ||
} | ||
} |
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,43 @@ | ||
package client_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/canonical/oci-factory/cli-client/internals/client" | ||
) | ||
|
||
func TestGetWorkflowRunStatusFromResp(t *testing.T) { | ||
mockResponseBody := []byte(`{"status":"completed","conclusion":"success"}`) | ||
expectedStatus := "completed" | ||
expectedConclusion := "success" | ||
|
||
status, conclusion := client.GetWorkflowRunStatusFromResp(mockResponseBody) | ||
|
||
// Verify the response | ||
if status != expectedStatus { | ||
t.Errorf("unexpected status, want %s, got %s", expectedStatus, status) | ||
} | ||
if conclusion != expectedConclusion { | ||
t.Errorf("unexpected conclusion, want %s, got %s", expectedConclusion, conclusion) | ||
} | ||
} | ||
|
||
func TestGetWorkflowJobsProgressFromResp(t *testing.T) { | ||
mockResponseBody := []byte(`{"jobs":[{"name":"Job 1","status":"completed"},{"name":"Job 2","status":"in_progress"},{"name":"Job 3","status":"queued"}]}`) | ||
expectedCurrJob := 2 | ||
expectedTotalJobs := 3 | ||
expectedJobName := "Job 2" | ||
|
||
currJob, totalJobs, jobName := client.GetWorkflowJobsProgressFromResp(mockResponseBody) | ||
|
||
// Verify the response | ||
if currJob != expectedCurrJob { | ||
t.Errorf("unexpected current job, want %d, got %d", expectedCurrJob, currJob) | ||
} | ||
if totalJobs != expectedTotalJobs { | ||
t.Errorf("unexpected total jobs, want %d, got %d", expectedTotalJobs, totalJobs) | ||
} | ||
if jobName != expectedJobName { | ||
t.Errorf("unexpected job name, want %s, got %s", expectedJobName, jobName) | ||
} | ||
} |
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,25 @@ | ||
package token_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/canonical/oci-factory/cli-client/internals/token" | ||
) | ||
|
||
func TestReadAccessTokenEnv(t *testing.T) { | ||
expectedToken := "ghp_test123ToKeN" | ||
saveToken := token.UpdateEnvToken(expectedToken) | ||
err := os.Setenv(token.TokenVarName, expectedToken) | ||
if err != nil { | ||
t.Fatalf("Unable to set env variable: %v", err) | ||
} | ||
resultToken := token.GetAccessToken() | ||
if resultToken != expectedToken { | ||
t.Fatalf("") | ||
} | ||
token.RestoreTokenEnv(saveToken) | ||
if os.Getenv(token.TokenVarName) != saveToken { | ||
t.Fatalf("Unable to restore saved env variable") | ||
} | ||
} |