Skip to content

Commit

Permalink
try ci tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AwesomeXjs committed Jan 20, 2025
1 parent 1d81ec2 commit b385439
Show file tree
Hide file tree
Showing 7 changed files with 726 additions and 39 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
- name: Run golangci-lint for Auth service
uses: golangci/golangci-lint-action@v6
with:
version: v1.60.3
args: --config=${{ github.workspace }}/auth-service/.golangci.pipeline.yaml ./...
working-directory: auth-service
version: v1.60.3
args: --config=${{ github.workspace }}/auth-service/.golangci.pipeline.yaml ./...
working-directory: auth-service

- name: Run golangci-lint for Auth-api-gateway service
uses: golangci/golangci-lint-action@v6
Expand All @@ -38,4 +38,21 @@ jobs:
with:
version: v1.60.3
args: --config=${{ github.workspace }}/mail-checking-service/.golangci.pipeline.yaml ./...
working-directory: mail-checking-service
working-directory: mail-checking-service

tests:
name: Test Auth Service
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22.2'
cache: false

- name: Run tests for Auth service
working-directory: auth-service
run: |
go test ./... -v
70 changes: 35 additions & 35 deletions auth-service/coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

<option value="file9">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/converter/to_proto.go (100.0%)</option>

<option value="file10">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/converter/user.go (0.0%)</option>
<option value="file10">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/converter/user.go (50.0%)</option>

<option value="file11">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/grpc_server/confirm_email.go (100.0%)</option>

Expand Down Expand Up @@ -131,19 +131,19 @@

<option value="file37">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/repository/init.go (0.0%)</option>

<option value="file38">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/confirm_email.go (0.0%)</option>
<option value="file38">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/confirm_email.go (100.0%)</option>

<option value="file39">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/get_access_token.go (0.0%)</option>
<option value="file39">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/get_access_token.go (100.0%)</option>

<option value="file40">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/init.go (0.0%)</option>

<option value="file41">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/login.go (0.0%)</option>
<option value="file41">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/login.go (100.0%)</option>

<option value="file42">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/registration.go (57.6%)</option>

<option value="file43">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/update_password.go (0.0%)</option>
<option value="file43">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/update_password.go (100.0%)</option>

<option value="file44">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/validate.go (0.0%)</option>
<option value="file44">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth/validate.go (100.0%)</option>

<option value="file45">github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/event_sender/scheduler.go (0.0%)</option>

Expand Down Expand Up @@ -876,7 +876,7 @@

// ToModelAuthResponse converts access and refresh tokens along with user ID
// into an AuthResponse model.
func ToModelAuthResponse(accessToken, refreshToken string, UserID int) *model.AuthResponse <span class="cov6" title="5">{
func ToModelAuthResponse(accessToken, refreshToken string, UserID int) *model.AuthResponse <span class="cov8" title="10">{
return &amp;model.AuthResponse{
AccessToken: accessToken,
RefreshToken: refreshToken,
Expand Down Expand Up @@ -1013,7 +1013,7 @@
}</span>

// FromUpdatePassInfoToDbPassInfo converts UpdatePassInfo and hashed password to UpdatePassDb model.
func FromUpdatePassInfoToDbPassInfo(info *model.UpdatePassInfo, hashedPassword string) *model.UpdatePassDb <span class="cov0" title="0">{
func FromUpdatePassInfoToDbPassInfo(info *model.UpdatePassInfo, hashedPassword string) *model.UpdatePassDb <span class="cov10" title="10">{
return &amp;model.UpdatePassDb{
Email: info.Email,
HashPassword: hashedPassword,
Expand Down Expand Up @@ -2312,7 +2312,7 @@

// ConfirmEmail is a service method that handles the email confirmation process.
// It calls the repository layer to update the email verification status.
func (s *ServiceAuth) ConfirmEmail(ctx context.Context, mail string) error <span class="cov0" title="0">{
func (s *ServiceAuth) ConfirmEmail(ctx context.Context, mail string) error <span class="cov10" title="10">{
return s.Repo.Auth.ConfirmEmail(ctx, mail)
}</span>
</pre>
Expand All @@ -2330,36 +2330,36 @@
)

// GetAccessToken generates a new access token and refresh token based on the provided refresh token.
func (s *ServiceAuth) GetAccessToken(ctx context.Context, refreshToken string) (*model.NewPairTokens, error) <span class="cov0" title="0">{
func (s *ServiceAuth) GetAccessToken(ctx context.Context, refreshToken string) (*model.NewPairTokens, error) <span class="cov10" title="25">{

const mark = "Service.Auth.GetAccessToken"

logger.Debug("getting refresh token on service", mark, zap.String("REFRESH_TOKEN", refreshToken))

claims, err := s.AuthHelper.VerifyToken(refreshToken)
if err != nil </span><span class="cov0" title="0">{
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to verify token", mark, zap.Error(err))
return nil, fmt.Errorf("failed to verify token: %v", err)
}</span>

<span class="cov0" title="0">info, err := s.Repo.Auth.GetAccessToken(ctx, claims.ID)
if err != nil </span><span class="cov0" title="0">{
<span class="cov9" title="20">info, err := s.Repo.Auth.GetAccessToken(ctx, claims.ID)
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to get access token", mark, zap.Error(err))
return nil, fmt.Errorf("failed to get access token: %v", err)
}</span>
<span class="cov0" title="0">access, err := s.AuthHelper.GenerateAccessToken(info)
if err != nil </span><span class="cov0" title="0">{
<span class="cov8" title="15">access, err := s.AuthHelper.GenerateAccessToken(info)
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to generate access token", mark, zap.Error(err))
return nil, fmt.Errorf("failed to generate access token: %v", err)
}</span>

<span class="cov0" title="0">refresh, err := s.AuthHelper.GenerateRefreshToken(info.ID)
if err != nil </span><span class="cov0" title="0">{
<span class="cov7" title="10">refresh, err := s.AuthHelper.GenerateRefreshToken(info.ID)
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to generate refresh token", mark, zap.Error(err))
return nil, fmt.Errorf("failed to generate refresh token: %v", err)
}</span>

<span class="cov0" title="0">return &amp;model.NewPairTokens{AccessToken: access, RefreshToken: refresh}, nil</span>
<span class="cov5" title="5">return &amp;model.NewPairTokens{AccessToken: access, RefreshToken: refresh}, nil</span>
}
</pre>

Expand Down Expand Up @@ -2434,40 +2434,40 @@
)

// Login authenticates a user with the provided login information and generates access and refresh tokens.
func (s *ServiceAuth) Login(ctx context.Context, loginInfo *model.LoginInfo) (*model.AuthResponse, error) <span class="cov0" title="0">{
func (s *ServiceAuth) Login(ctx context.Context, loginInfo *model.LoginInfo) (*model.AuthResponse, error) <span class="cov10" title="25">{

const mark = "Service.Auth.Login"

logger.Debug("get data in service", mark, zap.Any("loginInfo", loginInfo))

result, err := s.Repo.Auth.Login(ctx, loginInfo.Email)
if err != nil </span><span class="cov0" title="0">{
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to login", mark, zap.Error(err))
return nil, err
}</span>

<span class="cov0" title="0">ok := s.AuthHelper.ValidatePassword(result.HashPassword, loginInfo.Password)
if !ok </span><span class="cov0" title="0">{
<span class="cov9" title="20">ok := s.AuthHelper.ValidatePassword(result.HashPassword, loginInfo.Password)
if !ok </span><span class="cov5" title="5">{
logger.Error("invalid password", mark, zap.Error(err))
return nil, fmt.Errorf("invalid password")
}</span>

<span class="cov0" title="0">accessToken, err := s.AuthHelper.GenerateAccessToken(&amp;model.AccessTokenInfo{
<span class="cov8" title="15">accessToken, err := s.AuthHelper.GenerateAccessToken(&amp;model.AccessTokenInfo{
ID: result.UserID,
Role: result.Role,
})
if err != nil </span><span class="cov0" title="0">{
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to generate access token", mark, zap.Error(err))
return nil, fmt.Errorf("failed to generate access token: %v", err)
}</span>

<span class="cov0" title="0">refreshToken, err := s.AuthHelper.GenerateRefreshToken(result.UserID)
if err != nil </span><span class="cov0" title="0">{
<span class="cov7" title="10">refreshToken, err := s.AuthHelper.GenerateRefreshToken(result.UserID)
if err != nil </span><span class="cov5" title="5">{
logger.Error("failed to generate refresh token", mark, zap.Error(err))
return nil, fmt.Errorf("failed to generate refresh token: %v", err)
}</span>

<span class="cov0" title="0">logger.Debug("new pair tokens in service: ", mark, zap.Any("tokens", result))
<span class="cov5" title="5">logger.Debug("new pair tokens in service: ", mark, zap.Any("tokens", result))

return converter.ToModelAuthResponse(accessToken, refreshToken, result.UserID), nil</span>
}
Expand Down Expand Up @@ -2564,24 +2564,24 @@
)

// UpdatePassword hashes the new password and updates the user's password in the database.
func (s *ServiceAuth) UpdatePassword(ctx context.Context, updatePassInfo *model.UpdatePassInfo) error <span class="cov0" title="0">{
func (s *ServiceAuth) UpdatePassword(ctx context.Context, updatePassInfo *model.UpdatePassInfo) error <span class="cov10" title="15">{

const mark = "Service.Auth.UpdatePassword"

logger.Debug("get data in service", mark, zap.Any("updatePassInfo", updatePassInfo))

hashPassword, err := s.AuthHelper.HashPassword(updatePassInfo.NewPassword)
if err != nil </span><span class="cov0" title="0">{
if err != nil </span><span class="cov6" title="5">{
logger.Error("failed to hash password", mark, zap.Error(err))
return fmt.Errorf("failed to hash password: %v", err)
}</span>
<span class="cov0" title="0">err = s.Repo.Auth.UpdatePassword(ctx, converter.FromUpdatePassInfoToDbPassInfo(updatePassInfo, hashPassword))
if err != nil </span><span class="cov0" title="0">{
<span class="cov8" title="10">err = s.Repo.Auth.UpdatePassword(ctx, converter.FromUpdatePassInfoToDbPassInfo(updatePassInfo, hashPassword))
if err != nil </span><span class="cov6" title="5">{
logger.Error("failed to update password", mark, zap.Error(err))
return fmt.Errorf("failed to update password: %v", err)
}</span>

<span class="cov0" title="0">logger.Debug("password updated", mark, zap.Any("updatePassInfo", updatePassInfo))
<span class="cov6" title="5">logger.Debug("password updated", mark, zap.Any("updatePassInfo", updatePassInfo))
return nil</span>
}
</pre>
Expand All @@ -2598,18 +2598,18 @@
)

// ValidateToken verifies the access token and returns true if valid.
func (s *ServiceAuth) ValidateToken(_ context.Context, accessToken string) (bool, error) <span class="cov0" title="0">{
func (s *ServiceAuth) ValidateToken(_ context.Context, accessToken string) (bool, error) <span class="cov10" title="10">{

const mark = "Service.Auth.ValidateToken"

logger.Debug("get access token in service", mark, zap.String("ACCESS_TOKEN", accessToken))

_, err := s.AuthHelper.VerifyToken(accessToken)
if err != nil </span><span class="cov0" title="0">{
if err != nil </span><span class="cov7" title="5">{
logger.Error("failed to verify token", mark, zap.Error(err))
return false, fmt.Errorf("failed to verify token: %v", err)
}</span>
<span class="cov0" title="0">return true, nil</span>
<span class="cov7" title="5">return true, nil</span>
}
</pre>

Expand Down
85 changes: 85 additions & 0 deletions auth-service/tests/unit/service/confirm_mail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package service

import (
"context"
"fmt"
"testing"

"github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/app"
"github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/repository"
"github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/repository/auth"
serviceAuth "github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/internal/service/auth"
"github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/pkg/logger"
"github.com/AwesomeXjs/registration-service-with-checking-mail/auth-service/tests/unit/mocks"
"github.com/brianvoe/gofakeit"
"github.com/gojuno/minimock/v3"
"github.com/stretchr/testify/require"
)

func TestConfirmMail(t *testing.T) {
t.Parallel()
logger.Init(logger.GetCore(logger.GetAtomicLevel(app.LogLevel)))

type IRepositoryMockFunc func(mc *minimock.Controller) auth.IRepositoryAuth

type args struct {
ctx context.Context
mail string
}

var (
ctx = context.Background()
mc = minimock.NewController(t)

mail = gofakeit.Email()
)

defer t.Cleanup(mc.Finish)

tests := []struct {
name string
args args
err error
authRepoMock IRepositoryMockFunc
}{
{
name: "success",
args: args{
ctx: ctx,
mail: mail,
},
err: nil,
authRepoMock: func(mc *minimock.Controller) auth.IRepositoryAuth {
repo := mocks.NewIRepositoryAuthMock(mc)
repo.ConfirmEmailMock.Expect(ctx, mail).Return(nil)
return repo
},
},
{
name: "service error",
args: args{
ctx: ctx,
mail: mail,
},
err: fmt.Errorf("some error"),
authRepoMock: func(mc *minimock.Controller) auth.IRepositoryAuth {
repo := mocks.NewIRepositoryAuthMock(mc)
repo.ConfirmEmailMock.Expect(ctx, mail).Return(fmt.Errorf("some error"))
return repo
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
repositoryMock := &repository.Repository{Auth: tt.authRepoMock(mc)}
service := serviceAuth.ServiceAuth{Repo: repositoryMock}

err := service.ConfirmEmail(ctx, mail)
require.Equal(t, tt.err, err)

})
}
}
Loading

0 comments on commit b385439

Please sign in to comment.