As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Unverified Commit 4fc2f587 authored by Ash McKenzie's avatar Ash McKenzie Committed by GitLab
Browse files

Merge branch 'fix-lint-twofactorrecover' into 'main'

Fixes `make lint` (golangci-lint) issues for `internal/gitlabnet/twofactorrecover/client.go` and `internal/gitlabnet/twofactorrecover/client_test.go`

Closes #716

See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1009



Merged-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Approved-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Co-authored-by: default avatargaurav.marwal <gauravmarwal@gmail.com>
parents 8c70663e 418bc17c
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -33,7 +33,7 @@ func setup(t *testing.T) {
var requestBody *twofactorrecover.RequestBody
json.Unmarshal(b, &requestBody)
switch requestBody.KeyId {
switch requestBody.KeyID {
case "1":
body := map[string]interface{}{
"success": true,
Loading
Loading
// Package twofactorrecover provides functionality for interacting with GitLab Two-Factor Authentication recovery codes
package twofactorrecover
import (
Loading
Loading
@@ -13,31 +14,36 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/discover"
)
// Client represents a client for interacting with GitLab Two-Factor Authentication recovery codes
type Client struct {
config *config.Config
client *client.GitlabNetClient
}
// Response represents the response structure for Two-Factor Authentication recovery code requests
type Response struct {
Success bool `json:"success"`
RecoveryCodes []string `json:"recovery_codes"`
Message string `json:"message"`
}
// RequestBody represents the request body structure for Two-Factor Authentication recovery code requests
type RequestBody struct {
KeyId string `json:"key_id,omitempty"`
UserId int64 `json:"user_id,omitempty"`
KeyID string `json:"key_id,omitempty"`
UserID int64 `json:"user_id,omitempty"`
}
// NewClient creates a new Client instance with the provided configuration
func NewClient(config *config.Config) (*Client, error) {
client, err := gitlabnet.GetClient(config)
if err != nil {
return nil, fmt.Errorf("Error creating http client: %v", err)
return nil, fmt.Errorf("error creating http client: %v", err)
}
return &Client{config: config, client: client}, nil
}
// GetRecoveryCodes retrieves the recovery codes for the specified user
func (c *Client) GetRecoveryCodes(ctx context.Context, args *commandargs.Shell) ([]string, error) {
requestBody, err := c.getRequestBody(ctx, args)
Loading
Loading
@@ -49,7 +55,7 @@ func (c *Client) GetRecoveryCodes(ctx context.Context, args *commandargs.Shell)
if err != nil {
return nil, err
}
defer response.Body.Close()
defer func() { _ = response.Body.Close() }()
return parse(response)
}
Loading
Loading
@@ -76,7 +82,7 @@ func (c *Client) getRequestBody(ctx context.Context, args *commandargs.Shell) (*
var requestBody *RequestBody
if args.GitlabKeyId != "" {
requestBody = &RequestBody{KeyId: args.GitlabKeyId}
requestBody = &RequestBody{KeyID: args.GitlabKeyId}
} else {
userInfo, err := client.GetByCommandArgs(ctx, args)
Loading
Loading
@@ -84,7 +90,7 @@ func (c *Client) getRequestBody(ctx context.Context, args *commandargs.Shell) (*
return nil, err
}
requestBody = &RequestBody{UserId: userInfo.UserId}
requestBody = &RequestBody{UserID: userInfo.UserId}
}
return requestBody, nil
Loading
Loading
Loading
Loading
@@ -32,7 +32,7 @@ func initialize(t *testing.T) {
var requestBody *RequestBody
json.Unmarshal(b, &requestBody)
switch requestBody.KeyId {
switch requestBody.KeyID {
case "0":
body := map[string]interface{}{
"success": true,
Loading
Loading
@@ -57,7 +57,7 @@ func initialize(t *testing.T) {
w.WriteHeader(http.StatusForbidden)
}
if requestBody.UserId == 1 {
if requestBody.UserID == 1 {
body := map[string]interface{}{
"success": true,
"recovery_codes": [2]string{"recovery 2", "codes 2"},
Loading
Loading
@@ -68,7 +68,7 @@ func initialize(t *testing.T) {
},
{
Path: "/api/v4/internal/discover",
Handler: func(w http.ResponseWriter, r *http.Request) {
Handler: func(w http.ResponseWriter, _ *http.Request) {
body := &discover.Response{
UserId: 1,
Username: "jane-doe",
Loading
Loading
@@ -111,29 +111,29 @@ func TestErrorResponses(t *testing.T) {
testCases := []struct {
desc string
fakeId string
fakeID string
expectedError string
}{
{
desc: "A response with an error message",
fakeId: "2",
fakeID: "2",
expectedError: "Not allowed!",
},
{
desc: "A response with bad JSON",
fakeId: "3",
fakeID: "3",
expectedError: "Parsing failed",
},
{
desc: "An error response without message",
fakeId: "4",
fakeID: "4",
expectedError: "Internal API error (403)",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
args := &commandargs.Shell{GitlabKeyId: tc.fakeId}
args := &commandargs.Shell{GitlabKeyId: tc.fakeID}
resp, err := client.GetRecoveryCodes(context.Background(), args)
require.EqualError(t, err, tc.expectedError)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment