Skip to content

Commit 94be689

Browse files
authored
CLOUDP-344788: add auth command (#4254)
1 parent f564c09 commit 94be689

File tree

4 files changed

+298
-0
lines changed

4 files changed

+298
-0
lines changed

internal/cli/auth/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func Builder() *cobra.Command {
2929
WhoAmIBuilder(),
3030
LogoutBuilder(),
3131
RegisterBuilder(),
32+
TokenBuilder(),
3233
)
3334

3435
return cmd

internal/cli/auth/token.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2025 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/atlas-cli-core/config"
21+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
22+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=token_mock_test.go -package=auth . TokenConfig
27+
28+
type TokenConfig interface {
29+
AccessToken() string
30+
Name() string
31+
}
32+
33+
type tokenOpts struct {
34+
cli.OutputOpts
35+
config TokenConfig
36+
}
37+
38+
func (opts *tokenOpts) Run() error {
39+
accessToken := opts.config.AccessToken()
40+
if accessToken == "" {
41+
return fmt.Errorf("no access token found for profile %s", opts.config.Name())
42+
}
43+
return opts.Print(accessToken)
44+
}
45+
46+
func TokenBuilder() *cobra.Command {
47+
opts := &tokenOpts{}
48+
cmd := &cobra.Command{
49+
Use: "token",
50+
Hidden: true,
51+
Short: "Return the token for the current profile.",
52+
Example: ` # Return the token for the current profile:
53+
atlas auth token
54+
55+
# Return the token for the current profile and save it to a file:
56+
atlas auth token > token.txt
57+
58+
# Return the token for a specific profile:
59+
atlas auth token --profile <profile_name>
60+
`,
61+
Args: require.NoArgs,
62+
PreRunE: func(cmd *cobra.Command, _ []string) error {
63+
opts.OutWriter = cmd.OutOrStdout()
64+
// If the profile is set in the context, use it instead of the default profile
65+
profile, ok := config.ProfileFromContext(cmd.Context())
66+
if ok {
67+
opts.config = profile
68+
} else {
69+
opts.config = config.Default()
70+
}
71+
return nil
72+
},
73+
RunE: func(_ *cobra.Command, _ []string) error {
74+
return opts.Run()
75+
},
76+
}
77+
78+
opts.AddOutputOptFlags(cmd)
79+
80+
return cmd
81+
}

internal/cli/auth/token_mock_test.go

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cli/auth/token_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2025 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+
"bytes"
19+
"testing"
20+
21+
"github.com/mongodb/atlas-cli-core/config"
22+
"github.com/mongodb/atlas-cli-core/mocks"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
"go.uber.org/mock/gomock"
26+
)
27+
28+
func Test_tokenOpts_Run_Success(t *testing.T) {
29+
ctrl := gomock.NewController(t)
30+
defer ctrl.Finish()
31+
32+
mockConfig := NewMockTokenConfig(ctrl)
33+
buf := new(bytes.Buffer)
34+
35+
opts := &tokenOpts{
36+
config: mockConfig,
37+
}
38+
opts.OutWriter = buf
39+
opts.Output = "template" // Set output format to template
40+
41+
mockConfig.EXPECT().AccessToken().Return("test-access-token").Times(1)
42+
43+
err := opts.Run()
44+
require.NoError(t, err)
45+
assert.Equal(t, "test-access-token\n", buf.String())
46+
}
47+
48+
func Test_tokenOpts_Run_NoToken(t *testing.T) {
49+
ctrl := gomock.NewController(t)
50+
defer ctrl.Finish()
51+
52+
mockConfig := NewMockTokenConfig(ctrl)
53+
buf := new(bytes.Buffer)
54+
55+
opts := &tokenOpts{
56+
config: mockConfig,
57+
}
58+
opts.OutWriter = buf
59+
60+
mockConfig.EXPECT().AccessToken().Return("").Times(1)
61+
mockConfig.EXPECT().Name().Return("test-profile").Times(1)
62+
63+
err := opts.Run()
64+
require.Error(t, err)
65+
assert.Contains(t, err.Error(), "no access token found for profile test-profile")
66+
}
67+
68+
func TestTokenBuilder_PreRunE_DefaultConfig(t *testing.T) {
69+
cmd := TokenBuilder()
70+
71+
// Test that PreRunE uses config.Default() when no profile in context
72+
err := cmd.PreRunE(cmd, []string{})
73+
74+
// Should not error - just sets up the default config
75+
require.NoError(t, err)
76+
}
77+
78+
func TestTokenBuilder_PreRunE_ProfileFromContext(t *testing.T) {
79+
ctrl := gomock.NewController(t)
80+
defer ctrl.Finish()
81+
82+
mockStore := mocks.NewMockStore(ctrl)
83+
84+
// Create a test profile
85+
testProfile := config.NewProfile("test-profile", mockStore)
86+
87+
cmd := TokenBuilder()
88+
89+
// Add profile to context and execute the command with that context
90+
ctx := config.WithProfile(t.Context(), testProfile)
91+
cmd.SetContext(ctx)
92+
93+
mockStore.EXPECT().
94+
GetHierarchicalValue("test-profile", gomock.Any()).
95+
Return("").
96+
AnyTimes()
97+
98+
err := cmd.PreRunE(cmd, []string{})
99+
require.NoError(t, err)
100+
}

0 commit comments

Comments
 (0)