Skip to content

Commit f08e168

Browse files
authored
CLOUDP-120668: Add register command skeleton (#1149)
1 parent 4703b44 commit f08e168

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

internal/cli/auth/login.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func LoginBuilder() *cobra.Command {
190190
if hasUserProgrammaticKeys() {
191191
return fmt.Errorf(`you have already set the programmatic keys for this profile.
192192
193-
Run '%s auth login --profile <profile_name>' to use your username and password on a new profile`, config.BinName())
193+
Run '%s auth login --profile <profile_name>' to use your username and password with a new profile`, config.BinName())
194194
}
195195

196196
opts.OutWriter = cmd.OutOrStdout()
@@ -230,5 +230,9 @@ func Builder() *cobra.Command {
230230
LogoutBuilder(),
231231
)
232232

233+
if config.ToolName == config.AtlasCLI {
234+
cmd.AddCommand(RegisterBuilder())
235+
}
236+
233237
return cmd
234238
}

internal/cli/auth/login_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"testing"
2424

2525
"github.com/golang/mock/gomock"
26+
"github.com/mongodb/mongocli/internal/config"
2627
"github.com/mongodb/mongocli/internal/mocks"
2728
"github.com/mongodb/mongocli/internal/test"
2829
"github.com/stretchr/testify/assert"
@@ -40,6 +41,23 @@ func TestBuilder(t *testing.T) {
4041
)
4142
}
4243

44+
func TestBuilderForAtlas(t *testing.T) {
45+
// Test for AtlasCLI
46+
prevTool := config.ToolName
47+
config.ToolName = config.AtlasCLI
48+
49+
t.Cleanup(func() {
50+
config.ToolName = prevTool
51+
})
52+
53+
test.CmdValidator(
54+
t,
55+
Builder(),
56+
4,
57+
[]string{},
58+
)
59+
}
60+
4361
func TestLoginBuilder(t *testing.T) {
4462
test.CmdValidator(
4563
t,

internal/cli/auth/register.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2022 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package auth
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/mongodb/mongocli/internal/cli/require"
21+
"github.com/mongodb/mongocli/internal/config"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
type registerOpts struct {
26+
loginOpts
27+
}
28+
29+
func (opts *registerOpts) Run() error {
30+
_, _ = fmt.Fprintf(opts.OutWriter, "Create and verify your MongoDB Atlas account from the web browser and return to Atlas CLI after activating your account.\n")
31+
// TODO: CLOUDP-120669
32+
return nil
33+
}
34+
35+
func RegisterBuilder() *cobra.Command {
36+
opts := &registerOpts{}
37+
cmd := &cobra.Command{
38+
Use: "register",
39+
Short: "Register with MongoDB Atlas.",
40+
Hidden: true,
41+
Example: fmt.Sprintf(` To start the interactive setup:
42+
$ %s auth register
43+
`, config.BinName()),
44+
PreRunE: func(cmd *cobra.Command, args []string) error {
45+
if hasUserProgrammaticKeys() {
46+
return fmt.Errorf(`you have already set the programmatic keys for this profile.
47+
48+
Run '%s auth register --profile <profileName>' to use your username and password with a new profile`, config.BinName())
49+
}
50+
51+
opts.OutWriter = cmd.OutOrStdout()
52+
opts.config = config.Default()
53+
if config.OpsManagerURL() != "" {
54+
opts.OpsManagerURL = config.OpsManagerURL()
55+
}
56+
return opts.initFlow()
57+
},
58+
RunE: func(cmd *cobra.Command, args []string) error {
59+
return opts.Run()
60+
},
61+
Args: require.NoArgs,
62+
}
63+
64+
cmd.Flags().BoolVar(&opts.isGov, "gov", false, "Register to Atlas for Government.")
65+
cmd.Flags().BoolVar(&opts.noBrowser, "noBrowser", false, "Don't try to open a browser session.")
66+
cmd.Flags().BoolVar(&opts.skipConfig, "skipConfig", false, "Skip profile configuration.")
67+
68+
return cmd
69+
}

internal/cli/auth/register_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build unit
16+
// +build unit
17+
18+
package auth
19+
20+
import (
21+
"bytes"
22+
"testing"
23+
24+
"github.com/golang/mock/gomock"
25+
"github.com/mongodb/mongocli/internal/mocks"
26+
"github.com/mongodb/mongocli/internal/test"
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
func TestRegisterBuilder(t *testing.T) {
32+
test.CmdValidator(
33+
t,
34+
RegisterBuilder(),
35+
0,
36+
[]string{"gov", "noBrowser"},
37+
)
38+
}
39+
40+
func Test_registerOpts_Run(t *testing.T) {
41+
ctrl := gomock.NewController(t)
42+
mockFlow := mocks.NewMockAuthenticator(ctrl)
43+
mockConfig := mocks.NewMockLoginConfig(ctrl)
44+
mockStore := mocks.NewMockProjectOrgsLister(ctrl)
45+
defer ctrl.Finish()
46+
buf := new(bytes.Buffer)
47+
48+
loginOpts := &loginOpts{
49+
flow: mockFlow,
50+
config: mockConfig,
51+
noBrowser: true,
52+
skipConfig: true,
53+
}
54+
55+
opts := &registerOpts{
56+
*loginOpts,
57+
}
58+
59+
opts.OutWriter = buf
60+
opts.Store = mockStore
61+
62+
require.NoError(t, opts.Run())
63+
assert.Equal(t, `Create and verify your MongoDB Atlas account from the web browser and return to Atlas CLI after activating your account.
64+
`, buf.String())
65+
}

0 commit comments

Comments
 (0)