Skip to content

Commit

Permalink
Merge pull request #139 from LerianStudio/test/login
Browse files Browse the repository at this point in the history
Test/MZ-578
  • Loading branch information
maxwelbm authored Oct 22, 2024
2 parents 5a755c7 + 95b4fe0 commit 9abe368
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 12 deletions.
4 changes: 4 additions & 0 deletions components/mdz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ get-perfsprint-deps:
perfsprint: get-perfsprint-deps
perfsprint ./...

.PHONY: test
test:
go test ./...

.PHONY : build
build:
go version
Expand Down
70 changes: 70 additions & 0 deletions components/mdz/internal/domain/repository/auth_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

184 changes: 184 additions & 0 deletions components/mdz/internal/rest/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package rest

import (
"net/http"
"testing"

"github.com/LerianStudio/midaz/components/mdz/internal/model"
"github.com/LerianStudio/midaz/components/mdz/pkg/environment"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

func TestAuthenticateWithCredentials(t *testing.T) {
tests := []struct {
name string
username string
password string
mockResponse string
mockStatusCode int
expectError bool
expectedToken *model.TokenResponse
}{
{
name: "success",
username: "testuser",
password: "testpassword",
mockResponse: `{
"access_token": "mock_access_token",
"id_token": "mock_id_token",
"refresh_token": "mock_refresh_token",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "read write"
}`,
mockStatusCode: 200,
expectError: false,
expectedToken: &model.TokenResponse{
AccessToken: "mock_access_token",
IDToken: "mock_id_token",
RefreshToken: "mock_refresh_token",
TokenType: "Bearer",
ExpiresIn: 3600,
Scope: "read write",
},
},
{
name: "invalid credentials",
username: "invaliduser",
password: "wrongpassword",
mockResponse: `{"error": "invalid_credentials"}`,
mockStatusCode: 401,
expectError: true,
expectedToken: nil,
},
{
name: "server error",
username: "testuser",
password: "testpassword",
mockResponse: `{"error": "internal_server_error"}`,
mockStatusCode: 500,
expectError: true,
expectedToken: nil,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", "https://mock-api.com/api/login/oauth/access_token",
httpmock.NewStringResponder(tc.mockStatusCode, tc.mockResponse))

factory := &factory.Factory{
HTTPClient: &http.Client{},
Env: &environment.Env{
ClientID: "test-client-id",
ClientSecret: "test-client-secret",
URLAPIAuth: "https://mock-api.com",
},
}

authInstance := &Auth{
Factory: factory,
}

token, err := authInstance.AuthenticateWithCredentials(tc.username, tc.password)

if tc.expectError {
assert.Error(t, err)
assert.Nil(t, token)
} else {
assert.NoError(t, err)
assert.NotNil(t, token)
assert.Equal(t, tc.expectedToken.AccessToken, token.AccessToken)
assert.Equal(t, tc.expectedToken.IDToken, token.IDToken)
assert.Equal(t, tc.expectedToken.RefreshToken, token.RefreshToken)
assert.Equal(t, tc.expectedToken.TokenType, token.TokenType)
assert.Equal(t, tc.expectedToken.ExpiresIn, token.ExpiresIn)
assert.Equal(t, tc.expectedToken.Scope, token.Scope)
}

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["POST https://mock-api.com/api/login/oauth/access_token"])
})
}
}

func TestExchangeToken(t *testing.T) {
tests := []struct {
name string
code string
mockResponse string
mockStatusCode int
expectError bool
expectedToken *model.TokenResponse
}{
{
name: "success",
code: "valid_code",
mockResponse: `{
"access_token": "mock_access_token",
"id_token": "mock_id_token",
"refresh_token": "mock_refresh_token",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "read write"
}`,
mockStatusCode: 200,
expectError: false,
expectedToken: &model.TokenResponse{
AccessToken: "mock_access_token",
IDToken: "mock_id_token",
RefreshToken: "mock_refresh_token",
TokenType: "Bearer",
ExpiresIn: 3600,
Scope: "read write",
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", "https://mock-api.com/api/login/oauth/access_token",
httpmock.NewStringResponder(tc.mockStatusCode, tc.mockResponse))

factory := &factory.Factory{
HTTPClient: &http.Client{},
Env: &environment.Env{
ClientID: "test-client-id",
ClientSecret: "test-client-secret",
URLAPIAuth: "https://mock-api.com",
},
}

authInstance := &Auth{
Factory: factory,
}

token, err := authInstance.ExchangeToken(tc.code)

if tc.expectError {
assert.Error(t, err)
assert.Nil(t, token)
} else {
assert.NoError(t, err)
assert.NotNil(t, token)
assert.Equal(t, tc.expectedToken.AccessToken, token.AccessToken)
assert.Equal(t, tc.expectedToken.IDToken, token.IDToken)
assert.Equal(t, tc.expectedToken.RefreshToken, token.RefreshToken)
assert.Equal(t, tc.expectedToken.TokenType, token.TokenType)
assert.Equal(t, tc.expectedToken.ExpiresIn, token.ExpiresIn)
assert.Equal(t, tc.expectedToken.Scope, token.Scope)
}

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["POST https://mock-api.com/api/login/oauth/access_token"])
})
}
}
21 changes: 11 additions & 10 deletions components/mdz/pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
)

type factoryLogin struct {
factory *factory.Factory
username string
password string
token string
browser browser
auth repository.Auth
factory *factory.Factory
username string
password string
token string
browser browser
auth repository.Auth
tuiSelect func(message string, options []string) (string, error)
}

func validateCredentials(username, password string) error {
Expand All @@ -43,8 +44,7 @@ func (l *factoryLogin) runE(cmd *cobra.Command, _ []string) error {
return err
}

r := rest.Auth{Factory: l.factory}
_, err := r.AuthenticateWithCredentials(l.username, l.password)
_, err := l.auth.AuthenticateWithCredentials(l.username, l.password)

if err != nil {
return err
Expand Down Expand Up @@ -116,8 +116,9 @@ func (l *factoryLogin) execMethodLogin(answer string) error {

func NewCmdLogin(f *factory.Factory) *cobra.Command {
fVersion := factoryLogin{
factory: f,
auth: rest.NewAuth(f),
factory: f,
auth: rest.NewAuth(f),
tuiSelect: tui.Select,
}

cmd := &cobra.Command{
Expand Down
Loading

0 comments on commit 9abe368

Please sign in to comment.