Skip to content

Commit

Permalink
Merge branch 'main' into mcobbett-fix-get-users-me-percent
Browse files Browse the repository at this point in the history
  • Loading branch information
techcobweb committed Sep 25, 2024
2 parents 7ff7987 + 139f50a commit 303460c
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 32 deletions.
2 changes: 2 additions & 0 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ The `galasactl` tool can generate the following errors:
- GAL1162E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are: '{}'
- GAL1163E: The run named '{}' could not be deleted because it was not found by the Galasa service. Try listing runs using 'galasactl runs get' to identify the one you wish to delete
- GAL1164E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
- GAL1165E: '{}' is not supported as a valid login ID. Login ID should not contain spaces.
- GAL1166E: The loginId provided by the --user field cannot be an empty string.
- GAL1225E: Failed to open file '{}' cause: {}. Check that this file exists, and that you have read permissions.
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
Expand Down
3 changes: 2 additions & 1 deletion docs/generated/galasactl_auth_tokens_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ galasactl auth tokens get [flags]
### Options

```
-h, --help Displays the options for the 'auth tokens get' command.
-h, --help Displays the options for the 'auth tokens get' command.
--user string Optional. Retrieves a list of access tokens for the user with the given username.
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/galasactl_runs_submit_local.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ galasactl runs submit local [flags]
--debug When set (or true) the debugger pauses on startup and tries to connect to a Java debugger. The connection is established using the --debugMode and --debugPort values.
--debugMode string The mode to use when the --debug option causes the testcase to connect to a Java debugger. Valid values are 'listen' or 'attach'. 'listen' means the testcase JVM will pause on startup, waiting for the Java debugger to connect to the debug port (see the --debugPort option). 'attach' means the testcase JVM will pause on startup, trying to attach to a java debugger which is listening on the debug port. The default value is 'listen' but can be overridden by the 'galasactl.jvm.local.launch.debug.mode' property in the bootstrap file, which in turn can be overridden by this explicit parameter on the galasactl command.
--debugPort uint32 The port to use when the --debug option causes the testcase to connect to a java debugger. The default value used is 2970 which can be overridden by the 'galasactl.jvm.local.launch.debug.port' property in the bootstrap file, which in turn can be overridden by this explicit parameter on the galasactl command.
--galasaVersion string the version of galasa you want to use to run your tests. This should match the version of the galasa obr you built your test bundles against. (default "0.37.0")
--galasaVersion string the version of galasa you want to use to run your tests. This should match the version of the galasa obr you built your test bundles against. (default "0.38.0")
--gherkin strings Gherkin feature file URL. Should start with 'file://'.
-h, --help Displays the options for the 'runs submit local' command.
--localMaven string The url of a local maven repository are where galasa bundles can be loaded from on your local file system. Defaults to your home .m2/repository file. Please note that this should be in a URL form e.g. 'file:///Users/myuserid/.m2/repository', or 'file://C:/Users/myuserid/.m2/repository'
Expand Down
80 changes: 63 additions & 17 deletions pkg/auth/authTokensGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package auth
import (
"context"
"log"
"net/http"
"strings"

galasaErrors "github.com/galasa-dev/cli/pkg/errors"
"github.com/galasa-dev/cli/pkg/galasaapi"
Expand All @@ -19,38 +21,82 @@ import (
func GetTokens(
apiClient *galasaapi.APIClient,
console spi.Console,
loginId string,
) error {

authTokens, err := getAuthTokensFromRestApi(apiClient)
authTokens, err := getAuthTokensFromRestApi(apiClient, loginId)

if err == nil {
summaryFormatter := tokensformatter.NewTokenSummaryFormatter()
err = formatFetchedTokensAndWriteToConsole(authTokens, console)
}

return err
}

func getAuthTokensFromRestApi(apiClient *galasaapi.APIClient, loginId string) ([]galasaapi.AuthToken, error) {
var context context.Context = nil
var authTokens []galasaapi.AuthToken
var err error

apiCall := apiClient.AuthenticationAPIApi.GetTokens(context)

var outputText string
outputText, err = summaryFormatter.FormatTokens(authTokens)
if loginId != "" {

loginId, err = validateLoginIdFlag(loginId)

if err == nil {
console.WriteString(outputText)
apiCall = apiCall.LoginId(loginId)
}
}

if err == nil {

var tokens *galasaapi.AuthTokens
var resp *http.Response

tokens, resp, err = apiCall.Execute()

if err != nil {
log.Println("getAuthTokensFromRestApi - Failed to retrieve list of tokens from API server")
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_TOKEN_LIST_FROM_API_SERVER, err.Error())
} else {
defer resp.Body.Close()
authTokens = tokens.GetTokens()
log.Printf("getAuthTokensFromRestApi - %v tokens collected", len(authTokens))
}
}

return authTokens, err
}

func formatFetchedTokensAndWriteToConsole(authTokens []galasaapi.AuthToken, console spi.Console) error {

summaryFormatter := tokensformatter.NewTokenSummaryFormatter()

outputText, err := summaryFormatter.FormatTokens(authTokens)

if err == nil {
console.WriteString(outputText)
}

return err

}

func getAuthTokensFromRestApi(apiClient *galasaapi.APIClient) ([]galasaapi.AuthToken, error) {
var context context.Context = nil
var authTokens []galasaapi.AuthToken
func validateLoginIdFlag(loginId string) (string, error) {

tokens, resp, err := apiClient.AuthenticationAPIApi.GetTokens(context).Execute()
var err error

if err != nil {
log.Println("getAuthTokensFromRestApi - Failed to retrieve list of tokens from API server")
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_TOKEN_LIST_FROM_API_SERVER, err.Error())
} else {
defer resp.Body.Close()
authTokens = tokens.GetTokens()
log.Printf("getAuthTokensFromRestApi - %v tokens collected", len(authTokens))
loginId = strings.TrimSpace(loginId)
hasSpace := strings.Contains(loginId, " ")

if loginId == "" {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_USER_FLAG_VALUE)
}

return authTokens, err
if hasSpace {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_LOGIN_ID, loginId)
}

return loginId, err
}
92 changes: 89 additions & 3 deletions pkg/auth/authTokensGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ func mockAuthTokensServlet(t *testing.T, writer http.ResponseWriter, request *ht
body = `{
"tokens":[]
}`
} else if state == "missingLoginIdFlag" {
statusCode = 400
body = `{"error_code": 1155,"error_message": "GAL1155E: The id provided by the --id field cannot be an empty string."}`
} else if state == "invalidLoginIdFlag" {
statusCode = 400
body = `{"error_code": 1157,"error_message": "GAL1157E: '%s' is not supported as a valid value. Valid value should not contain spaces. A value of 'admin' is valid but 'galasa admin' is not."}`
} else if state == "populatedByLoginId" {
body = `{
"tokens":[
{
"token_id":"098234980123-1283182389",
"creation_time":"2023-12-03T18:25:43.511Z",
"owner": {
"login_id":"mcobbett"
},
"description":"So I can access ecosystem1 from my laptop."
},
{
"token_id":"8218971d287s1-dhj32er2323",
"creation_time":"2024-03-03T09:36:50.511Z",
"owner": {
"login_id":"mcobbett"
},
"description":"Automated build of example repo can change CPS properties"
}
]
}`
} else {
statusCode = 500
body = `{"error_code": 5000,"error_message": "GAL5000E: Error occured when trying to access the endpoint. Report the problem to your Galasa Ecosystem owner."}`
Expand All @@ -86,7 +113,7 @@ Total:3
`

//When
err := GetTokens(apiClient, console)
err := GetTokens(apiClient, console, "")

//Then
assert.Nil(t, err)
Expand All @@ -104,7 +131,7 @@ func TestNoTokensPathReturnsOk(t *testing.T) {
expectedOutput := "Total:0\n"

//When
err := GetTokens(apiClient, console)
err := GetTokens(apiClient, console, "")

//Then
assert.Nil(t, err)
Expand All @@ -121,10 +148,69 @@ func TestInvalidPathReturnsError(t *testing.T) {
console := utils.NewMockConsole()

//When
err := GetTokens(apiClient, console)
err := GetTokens(apiClient, console, "admin")

//Then
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "GAL1146E")
assert.Contains(t, err.Error(), "Could not get list of tokens from API server")
}

func TestMissingLoginIdFlagReturnsBadRequest(t *testing.T) {
//Given...
serverState := "missingLoginId"
server := NewAuthTokensServletMock(t, serverState)
apiClient := api.InitialiseAPI(server.URL)
defer server.Close()

console := utils.NewMockConsole()
expectedOutput := `GAL1166E: The loginId provided by the --user field cannot be an empty string.`

//When
err := GetTokens(apiClient, console, " ")

//Then
assert.NotNil(t, err)
assert.Equal(t, expectedOutput, err.Error())
}

func TestLoginIdWithSpacesReturnsBadRequest(t *testing.T) {
//Given...
serverState := "invalidLoginIdFlag"
server := NewAuthTokensServletMock(t, serverState)
apiClient := api.InitialiseAPI(server.URL)
defer server.Close()

console := utils.NewMockConsole()
expectedOutput := `GAL1165E: 'galasa admin' is not supported as a valid login ID. Login ID should not contain spaces.`

//When
err := GetTokens(apiClient, console, "galasa admin")

//Then
assert.NotNil(t, err)
assert.Equal(t, expectedOutput, err.Error())
}

func TestGetTokensByLoginIdReturnsOK(t *testing.T) {
//Given...
serverState := "populatedByLoginId"
server := NewAuthTokensServletMock(t, serverState)
apiClient := api.InitialiseAPI(server.URL)
defer server.Close()

console := utils.NewMockConsole()
expectedOutput := `tokenid created(YYYY-MM-DD) user description
098234980123-1283182389 2023-12-03 mcobbett So I can access ecosystem1 from my laptop.
8218971d287s1-dhj32er2323 2024-03-03 mcobbett Automated build of example repo can change CPS properties
Total:2
`

//When
err := GetTokens(apiClient, console, "mcobbett")

//Then
assert.Nil(t, err)
assert.Equal(t, expectedOutput, console.ReadText())
}
2 changes: 2 additions & 0 deletions pkg/cmd/authTokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type AuthTokensCmdValues struct {
bootstrap string
loginId string
}

type AuthTokensCommand struct {
Expand Down Expand Up @@ -76,3 +77,4 @@ func (cmd *AuthTokensCommand) createAuthTokensCobraCmd(

return authTokensCmd, err
}

13 changes: 11 additions & 2 deletions pkg/cmd/authTokensGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (cmd *AuthTokensGetCommand) createCobraCmd(

var err error

authTokensGetCommandValues := authTokensCommand.Values().(*AuthTokensCmdValues)
authGetTokensCobraCmd := &cobra.Command{
Use: "get",
Short: "Get a list of authentication tokens",
Expand All @@ -84,6 +85,7 @@ func (cmd *AuthTokensGetCommand) createCobraCmd(
},
}

addLoginIdFlagToAuthTokensGet(authGetTokensCobraCmd, authTokensGetCommandValues)
authTokensCommand.CobraCommand().AddCommand(authGetTokensCobraCmd)

return authGetTokensCobraCmd, err
Expand Down Expand Up @@ -133,12 +135,19 @@ func (cmd *AuthTokensGetCommand) executeAuthTokensGet(
apiClient, err = authenticator.GetAuthenticatedAPIClient()

if err == nil {
// Call to process the command in a unit-testable way.
err = auth.GetTokens(apiClient, console)
err = auth.GetTokens(apiClient, console, authTokenCmdValues.loginId)
}
}
}
}

return err
}

func addLoginIdFlagToAuthTokensGet(cmd *cobra.Command, authTokensGetCmdValues *AuthTokensCmdValues) {

flagName := "user"
var description string = "Optional. Retrieves a list of access tokens for the user with the given username."

cmd.Flags().StringVar(&authTokensGetCmdValues.loginId, flagName, "", description)
}
18 changes: 10 additions & 8 deletions pkg/errors/errorMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,18 @@ var (
GALASA_ERROR_MISSING_USER_LOGIN_ID_FLAG = NewMessageType("GAL1155E: The id provided by the --id field cannot be an empty string.", 1155, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_LOGIN_ID_NOT_SUPPORTED = NewMessageType("GAL1156E: '%s' is not supported as a valid value. Valid values are 'me'.", 1156, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUN_FAILED = NewMessageType("GAL1157E: An attempt to delete a run named '%s' failed. Cause is %s", 1157, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUNS_FAILED = NewMessageType("GAL1158E: An attempt to delete a run named '%s' failed. Sending the delete request to the Galasa service failed. Cause is %v", 1158, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUNS_FAILED = NewMessageType("GAL1158E: An attempt to delete a run named '%s' failed. Sending the delete request to the Galasa service failed. Cause is %v", 1158, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_LOGIN_ID = NewMessageType("GAL1165E: '%s' is not supported as a valid login ID. Login ID should not contain spaces.", 1165, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_USER_FLAG_VALUE = NewMessageType("GAL1166E: The loginId provided by the --user field cannot be an empty string.", 1166, STACK_TRACE_NOT_WANTED)

// 4 related but slightly different errors, when an HTTP response arrives from the Galasa server, and we can/can't parse the payload to get the message details out.
GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT = NewMessageType("GAL1159E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server.", 1159, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_RESPONSE_PAYLOAD_UNREADABLE = NewMessageType("GAL1160E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1160, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_UNPARSEABLE_CONTENT = NewMessageType("GAL1161E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1161, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_SERVER_REPORTED_ERROR = NewMessageType("GAL1162E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1162, STACK_TRACE_NOT_WANTED)
// 4 related but slightly different errors, when an HTTP response arrives from the Galasa server, and we can/can't parse the payload to get the message details out.
GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT = NewMessageType("GAL1159E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server.", 1159, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_RESPONSE_PAYLOAD_UNREADABLE = NewMessageType("GAL1160E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1160, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_UNPARSEABLE_CONTENT = NewMessageType("GAL1161E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1161, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_SERVER_REPORTED_ERROR = NewMessageType("GAL1162E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1162, STACK_TRACE_NOT_WANTED)

GALASA_ERROR_SERVER_DELETE_RUN_NOT_FOUND = NewMessageType("GAL1163E: The run named '%s' could not be deleted because it was not found by the Galasa service. Try listing runs using 'galasactl runs get' to identify the one you wish to delete", 1163, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_EXPLANATION_NOT_JSON = NewMessageType("GAL1164E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1164, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUN_NOT_FOUND = NewMessageType("GAL1163E: The run named '%s' could not be deleted because it was not found by the Galasa service. Try listing runs using 'galasactl runs get' to identify the one you wish to delete", 1163, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_EXPLANATION_NOT_JSON = NewMessageType("GAL1164E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1164, STACK_TRACE_NOT_WANTED)

// Warnings...
GALASA_WARNING_MAVEN_NO_GALASA_OBR_REPO = NewMessageType("GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '%s', and 'pre-release' repository is '%s'", 2000, STACK_TRACE_WANTED)
Expand Down
3 changes: 3 additions & 0 deletions test-galasactl-ecosystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ properties_tests
source ${BASEDIR}/test-scripts/resources-tests.sh --bootstrap "${bootstrap}"
resources_tests

source ${BASEDIR}/test-scripts/auth-tests.sh --bootstrap "${bootstrap}"
auth_tests

# Test the hybrid configuration where the local test runs locally, but
# draws it's CPS properties from a remote ecosystem via a REST extension.
source ${BASEDIR}/test-scripts/test-local-run-remote-cps.sh
Expand Down
Loading

0 comments on commit 303460c

Please sign in to comment.