-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9048cf0
commit 26845e9
Showing
10 changed files
with
294 additions
and
2 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
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rwx-research/mint-cli/internal/cli" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
WhoamiJson bool | ||
|
||
whoamiCmd = &cobra.Command{ | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return requireAccessToken() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := service.Whoami(cli.WhoamiConfig{Json: WhoamiJson, Stdout: os.Stdout}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
|
||
}, | ||
Short: "Outputs details about the access token in use", | ||
Use: "whoami [flags]", | ||
} | ||
) | ||
|
||
func init() { | ||
whoamiCmd.Flags().BoolVar(&WhoamiJson, "json", false, "output JSON instead of a textual representation") | ||
} |
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 |
---|---|---|
|
@@ -116,4 +116,34 @@ var _ = Describe("API Client", func() { | |
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("Whoami", func() { | ||
It("makes the request", func() { | ||
email := "[email protected]" | ||
body := struct { | ||
OrganizationSlug string `json:"organization_slug"` | ||
TokenKind string `json:"token_kind"` | ||
UserEmail *string `json:"user_email,omitempty"` | ||
}{ | ||
OrganizationSlug: "some-org", | ||
TokenKind: "personal_access_token", | ||
UserEmail: &email, | ||
} | ||
bodyBytes, _ := json.Marshal(body) | ||
|
||
roundTrip := func(req *http.Request) (*http.Response, error) { | ||
Expect(req.URL.Path).To(Equal("/api/auth/whoami")) | ||
return &http.Response{ | ||
Status: "200 OK", | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewReader(bodyBytes)), | ||
}, nil | ||
} | ||
|
||
c := api.Client{roundTrip} | ||
|
||
_, err := c.Whoami() | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |
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
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
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 |
---|---|---|
|
@@ -630,4 +630,152 @@ AAAEC6442PQKevgYgeT0SIu9zwlnEMl6MF59ZgM+i0ByMv4eLJPqG3xnZcEQmktHj/GY2i | |
}) | ||
}) | ||
}) | ||
|
||
Describe("whoami", func() { | ||
var ( | ||
stdout strings.Builder | ||
) | ||
|
||
BeforeEach(func() { | ||
stdout = strings.Builder{} | ||
}) | ||
|
||
Context("when outputting json", func() { | ||
Context("when the request fails", func() { | ||
BeforeEach(func() { | ||
mockAPI.MockWhoami = func() (*api.WhoamiResult, error) { | ||
return nil, errors.New("uh oh can't figure out who you are") | ||
} | ||
}) | ||
|
||
It("returns an error", func() { | ||
err := service.Whoami(cli.WhoamiConfig{ | ||
Json: true, | ||
Stdout: &stdout, | ||
}) | ||
|
||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("unable to determine details about the access token")) | ||
Expect(err.Error()).To(ContainSubstring("can't figure out who you are")) | ||
}) | ||
}) | ||
|
||
Context("when there is an email", func() { | ||
BeforeEach(func() { | ||
mockAPI.MockWhoami = func() (*api.WhoamiResult, error) { | ||
email := "[email protected]" | ||
return &api.WhoamiResult{ | ||
TokenKind: "personal_access_token", | ||
OrganizationSlug: "rwx", | ||
UserEmail: &email, | ||
}, nil | ||
} | ||
}) | ||
|
||
It("writes the token kind, organization, and user", func() { | ||
err := service.Whoami(cli.WhoamiConfig{ | ||
Json: true, | ||
Stdout: &stdout, | ||
}) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(stdout.String()).To(ContainSubstring(`"token_kind": "personal_access_token"`)) | ||
Expect(stdout.String()).To(ContainSubstring(`"organization_slug": "rwx"`)) | ||
Expect(stdout.String()).To(ContainSubstring(`"user_email": "[email protected]"`)) | ||
}) | ||
}) | ||
|
||
Context("when there is not an email", func() { | ||
BeforeEach(func() { | ||
mockAPI.MockWhoami = func() (*api.WhoamiResult, error) { | ||
return &api.WhoamiResult{ | ||
TokenKind: "organization_access_token", | ||
OrganizationSlug: "rwx", | ||
}, nil | ||
} | ||
}) | ||
|
||
It("writes the token kind and organization", func() { | ||
err := service.Whoami(cli.WhoamiConfig{ | ||
Json: true, | ||
Stdout: &stdout, | ||
}) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(stdout.String()).To(ContainSubstring(`"token_kind": "organization_access_token"`)) | ||
Expect(stdout.String()).To(ContainSubstring(`"organization_slug": "rwx"`)) | ||
Expect(stdout.String()).NotTo(ContainSubstring(`"user_email"`)) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("when outputting plaintext", func() { | ||
Context("when the request fails", func() { | ||
BeforeEach(func() { | ||
mockAPI.MockWhoami = func() (*api.WhoamiResult, error) { | ||
return nil, errors.New("uh oh can't figure out who you are") | ||
} | ||
}) | ||
|
||
It("returns an error", func() { | ||
err := service.Whoami(cli.WhoamiConfig{ | ||
Json: false, | ||
Stdout: &stdout, | ||
}) | ||
|
||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("unable to determine details about the access token")) | ||
Expect(err.Error()).To(ContainSubstring("can't figure out who you are")) | ||
}) | ||
}) | ||
|
||
Context("when there is an email", func() { | ||
BeforeEach(func() { | ||
mockAPI.MockWhoami = func() (*api.WhoamiResult, error) { | ||
email := "[email protected]" | ||
return &api.WhoamiResult{ | ||
TokenKind: "personal_access_token", | ||
OrganizationSlug: "rwx", | ||
UserEmail: &email, | ||
}, nil | ||
} | ||
}) | ||
|
||
It("writes the token kind, organization, and user", func() { | ||
err := service.Whoami(cli.WhoamiConfig{ | ||
Json: false, | ||
Stdout: &stdout, | ||
}) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(stdout.String()).To(ContainSubstring("Token Kind: personal access token")) | ||
Expect(stdout.String()).To(ContainSubstring("Organization: rwx")) | ||
Expect(stdout.String()).To(ContainSubstring("User: [email protected]")) | ||
}) | ||
}) | ||
|
||
Context("when there is not an email", func() { | ||
BeforeEach(func() { | ||
mockAPI.MockWhoami = func() (*api.WhoamiResult, error) { | ||
return &api.WhoamiResult{ | ||
TokenKind: "organization_access_token", | ||
OrganizationSlug: "rwx", | ||
}, nil | ||
} | ||
}) | ||
|
||
It("writes the token kind and organization", func() { | ||
err := service.Whoami(cli.WhoamiConfig{ | ||
Json: false, | ||
Stdout: &stdout, | ||
}) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(stdout.String()).To(ContainSubstring("Token Kind: organization access token")) | ||
Expect(stdout.String()).To(ContainSubstring("Organization: rwx")) | ||
Expect(stdout.String()).NotTo(ContainSubstring("User:")) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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