-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add command to create a test user for automated testing #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8b27054
feat: add command to create a test user for automated testing
OliverTrautvetter 71a38c5
Merge branch 'main' into create_test_user
OliverTrautvetter 99f814c
ref: fix linter
OliverTrautvetter 2e0105b
ref: enhance test user creation logic and add password generation tests
OliverTrautvetter 68e1313
Merge branch 'main' into create_test_user
OliverTrautvetter e6a7d70
Merge branch 'main' into create_test_user
OliverTrautvetter e26e487
chore(docs): Auto-update docs and licenses
OliverTrautvetter 11b5982
ref: streamline test user creation and update environment variable re…
OliverTrautvetter b26d2c0
Merge branch 'main' into create_test_user
OliverTrautvetter dc580ca
chore(docs): Auto-update docs and licenses
OliverTrautvetter 87808bd
ref: enhance error handling in RunCommand for quiet mode
OliverTrautvetter d99fd2f
Merge branch 'create_test_user' of https://github.com/codesphere-clou…
OliverTrautvetter d8e2d71
ref: streamline test user creation and remove unused password hashing…
OliverTrautvetter 0835b78
Merge branch 'main' into create_test_user
OliverTrautvetter 4db0b31
chore(docs): Auto-update docs and licenses
OliverTrautvetter f0b119f
Merge branch 'main' into create_test_user
OliverTrautvetter f3932a3
fix: update default value for registry-type flag in bootstrap GCP com…
OliverTrautvetter a2cc757
chore(docs): Auto-update docs and licenses
OliverTrautvetter 0fe35ce
fix: remove unused salt parameter from hashSecret function
OliverTrautvetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // CreateCmd represents the create command group | ||
| type CreateCmd struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func AddCreateCmd(rootCmd *cobra.Command, opts *GlobalOptions) { | ||
| create := CreateCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create resources for Codesphere", | ||
| Long: io.Long(`Create resources for Codesphere installations, such as test users for automated testing.`), | ||
| }, | ||
| } | ||
| AddCmd(rootCmd, create.cmd) | ||
|
|
||
| AddCreateTestUserCmd(create.cmd, opts) | ||
| } |
This file contains hidden or 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,69 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/codesphere-cloud/oms/internal/env" | ||
| "github.com/codesphere-cloud/oms/internal/testuser" | ||
| "github.com/codesphere-cloud/oms/internal/util" | ||
| ) | ||
|
|
||
| type CreateTestUserCmd struct { | ||
| cmd *cobra.Command | ||
| Opts CreateTestUserOpts | ||
| Env env.Env | ||
| } | ||
|
|
||
| type CreateTestUserOpts struct { | ||
| *GlobalOptions | ||
| testuser.CreateTestUserOpts | ||
| } | ||
|
|
||
| func (c *CreateTestUserCmd) RunE(_ *cobra.Command, args []string) error { | ||
| result, err := testuser.CreateTestUser(c.Opts.CreateTestUserOpts) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create test user: %w", err) | ||
| } | ||
|
|
||
| testuser.LogAndPersistResult(result, c.Env.GetOmsWorkdir()) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func AddCreateTestUserCmd(parent *cobra.Command, opts *GlobalOptions) { | ||
| c := CreateTestUserCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "test-user", | ||
| Short: "Create a test user on a Codesphere database", | ||
| Long: io.Long(`Creates a test user with a hashed password and API token directly in a Codesphere | ||
| PostgreSQL database. The user can be used for automated smoke tests. | ||
|
|
||
| The command connects to the specified PostgreSQL instance and creates the necessary | ||
| database records (credentials, email confirmation, team, team membership, API token). | ||
|
|
||
| Credentials are displayed and saved to the OMS workdir as test-user.json.`), | ||
| }, | ||
| Opts: CreateTestUserOpts{GlobalOptions: opts}, | ||
| Env: env.NewEnv(), | ||
| } | ||
| c.cmd.RunE = c.RunE | ||
|
|
||
| flags := c.cmd.Flags() | ||
| flags.StringVar(&c.Opts.Host, "postgres-host", "", "PostgreSQL host address (required)") | ||
| flags.IntVar(&c.Opts.Port, "postgres-port", testuser.DefaultPort, "PostgreSQL port") | ||
| flags.StringVar(&c.Opts.User, "postgres-user", testuser.DefaultUser, "PostgreSQL username") | ||
| flags.StringVar(&c.Opts.Password, "postgres-password", "", "PostgreSQL password (required)") | ||
| flags.StringVar(&c.Opts.DBName, "postgres-db", testuser.DefaultDBName, "PostgreSQL database name") | ||
| flags.StringVar(&c.Opts.SSLMode, "ssl-mode", testuser.DefaultSSLMode, "PostgreSQL SSL mode") | ||
|
|
||
| util.MarkFlagRequired(c.cmd, "postgres-host") | ||
| util.MarkFlagRequired(c.cmd, "postgres-password") | ||
|
|
||
| AddCmd(parent, c.cmd) | ||
| } | ||
This file contains hidden or 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,96 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd_test | ||
|
|
||
| import ( | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/codesphere-cloud/oms/cli/cmd" | ||
| ) | ||
|
|
||
| var _ = Describe("CreateTestUser", func() { | ||
| Context("AddCreateTestUserCmd", func() { | ||
| var createCmd cobra.Command | ||
| var opts *cmd.GlobalOptions | ||
|
|
||
| BeforeEach(func() { | ||
| createCmd = cobra.Command{} | ||
| opts = &cmd.GlobalOptions{} | ||
| }) | ||
|
|
||
| It("accepts valid flags with all required flags set", func() { | ||
| createCmd.SetArgs([]string{ | ||
| "test-user", | ||
| "--postgres-host", "localhost", | ||
| "--postgres-password", "secret", | ||
| }) | ||
|
|
||
| cmd.AddCreateTestUserCmd(&createCmd, opts) | ||
|
|
||
| createCmd.Commands()[0].RunE = func(cmd *cobra.Command, args []string) error { | ||
| return nil | ||
| } | ||
|
|
||
| err := createCmd.Execute() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| }) | ||
|
|
||
| It("fails when --postgres-host is missing", func() { | ||
| createCmd.SetArgs([]string{ | ||
| "test-user", | ||
| "--postgres-password", "secret", | ||
| }) | ||
|
|
||
| cmd.AddCreateTestUserCmd(&createCmd, opts) | ||
|
|
||
| createCmd.Commands()[0].RunE = func(cmd *cobra.Command, args []string) error { | ||
| return nil | ||
| } | ||
|
|
||
| err := createCmd.Execute() | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("postgres-host")) | ||
| }) | ||
|
|
||
| It("fails when --postgres-password is missing", func() { | ||
| createCmd.SetArgs([]string{ | ||
| "test-user", | ||
| "--postgres-host", "localhost", | ||
| }) | ||
|
|
||
| cmd.AddCreateTestUserCmd(&createCmd, opts) | ||
|
|
||
| createCmd.Commands()[0].RunE = func(cmd *cobra.Command, args []string) error { | ||
| return nil | ||
| } | ||
|
|
||
| err := createCmd.Execute() | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("postgres-password")) | ||
| }) | ||
|
|
||
| It("accepts optional flags with custom values", func() { | ||
| createCmd.SetArgs([]string{ | ||
| "test-user", | ||
| "--postgres-host", "db.example.com", | ||
| "--postgres-password", "secret", | ||
| "--postgres-port", "5433", | ||
| "--postgres-user", "admin", | ||
| "--postgres-db", "mydb", | ||
| "--ssl-mode", "require", | ||
| }) | ||
|
|
||
| cmd.AddCreateTestUserCmd(&createCmd, opts) | ||
|
|
||
| createCmd.Commands()[0].RunE = func(cmd *cobra.Command, args []string) error { | ||
| return nil | ||
| } | ||
|
|
||
| err := createCmd.Execute() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| ## oms create | ||
|
|
||
| Create resources for Codesphere | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Create resources for Codesphere installations, such as test users for automated testing. | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for create | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [oms](oms.md) - Codesphere Operations Management System (OMS) | ||
| * [oms create test-user](oms_create_test-user.md) - Create a test user on a Codesphere database | ||
|
|
This file contains hidden or 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,34 @@ | ||
| ## oms create test-user | ||
|
|
||
| Create a test user on a Codesphere database | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Creates a test user with a hashed password and API token directly in a Codesphere | ||
| PostgreSQL database. The user can be used for automated smoke tests. | ||
|
|
||
| The command connects to the specified PostgreSQL instance and creates the necessary | ||
| database records (credentials, email confirmation, team, team membership, API token). | ||
|
|
||
| Credentials are displayed and saved to the OMS workdir as test-user.json. | ||
|
|
||
| ``` | ||
| oms create test-user [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for test-user | ||
| --postgres-db string PostgreSQL database name (default "codesphere") | ||
| --postgres-host string PostgreSQL host address (required) | ||
| --postgres-password string PostgreSQL password (required) | ||
| --postgres-port int PostgreSQL port (default 5432) | ||
| --postgres-user string PostgreSQL username (default "postgres") | ||
| --ssl-mode string PostgreSQL SSL mode (default "disable") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [oms create](oms_create.md) - Create resources for Codesphere | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.