Skip to content

Commit

Permalink
Unit and Integration testing done
Browse files Browse the repository at this point in the history
  • Loading branch information
komalreddy3 committed Feb 1, 2024
1 parent e3dfeaa commit eb3a67b
Show file tree
Hide file tree
Showing 31 changed files with 1,825 additions and 435 deletions.
4 changes: 3 additions & 1 deletion api/loginApi/loginResthandler/loginResthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (impl LoginRestHandler) Login(w http.ResponseWriter, r *http.Request) {
</html>
`)
err := tmpl.Execute(w, nil)
impl.logger.Errorw("Execution of login went wrong", err)
if err != nil {
impl.logger.Errorw("Execution of login went wrong", err)
}

}
func (impl LoginRestHandler) Logout(w http.ResponseWriter, r *http.Request) {
Expand Down
36 changes: 27 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@ module github.com/komalreddy3/Attendance-go
go 1.21.6

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-pg/pg/v10 v10.12.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-pg/pg/v10 v10.12.0
github.com/google/wire v0.5.0
github.com/gorilla/mux v1.8.1
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.17.0
)

require (
github.com/DATA-DOG/go-sqlmock v1.5.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-pg/zerochecker v0.2.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.0 // indirect
github.com/jackc/puddle v1.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/pashagolub/pgxmock v1.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/bufpool v0.1.11 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mellium.im/sasl v0.3.1 // indirect
)
214 changes: 208 additions & 6 deletions go.sum

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions pkg/login/loginRepository/loginModels/loginModels.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ const (
)

type User struct {
ID string `json:"id" pg:",pk"`
Username string `json:"username"`
Password string `json:"password"`
Role UserRoleType `json:"role"` // "principal", "teacher", or "student"
tableName struct{} `sql:"user"`
ID string `json:"id" pg:",pk"`
Username string `json:"username"`
Password string `json:"password"`
Role UserRoleType `json:"role"` // "principal", "teacher", or "student"
}

func (j JWT) Create(content interface{}) (string, error) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/login/loginRepository/loginRepository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loginRepository

import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/komalreddy3/Attendance-go/pkg/login/loginRepository/loginModels"
"go.uber.org/zap"
Expand Down Expand Up @@ -58,12 +59,16 @@ func (impl LoginRepository) AuthenticateUser(username, password, userRole string
err := impl.dbConnection.Model(&user).
Where("username = ? AND role = ?", username, role).
Select()
fmt.Println("Here")
fmt.Println(err)
if err != nil {
return false
}

// Compare the hashed password
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
fmt.Println("Here2")
fmt.Println(err)
return err == nil
}
func (impl LoginRepository) CheckRole(content interface{}) loginModels.UserRoleType {
Expand Down
78 changes: 78 additions & 0 deletions pkg/login/loginRepository/mock_login_repository.go

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

3 changes: 2 additions & 1 deletion pkg/login/loginServices/loginServices.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ func (impl *LoginServiceImpl) AuthenticateRole(cookie *http.Cookie, check string
func (impl *LoginServiceImpl) Login(username, password, userRole string) string {
userID, err := impl.loginRepository.CheckCreds(username, userRole)
if err != nil {
impl.logger.Errorw("Invalid login credentials", err)
impl.logger.Errorw("Invalid login credentials first thing", err)
return ""
}
if impl.loginRepository.AuthenticateUser(username, password, userRole) {
fmt.Println("Entered")
prvKey, err := os.ReadFile("private.pem")
if err != nil {
log.Fatalln(err)
Expand Down
66 changes: 66 additions & 0 deletions pkg/login/loginServices/loginServices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package loginServices

import (
"github.com/golang/mock/gomock"
"github.com/komalreddy3/Attendance-go/pkg/login/loginRepository"
"github.com/komalreddy3/Attendance-go/pkg/login/loginRepository/loginModels"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"net/http"
"testing"
)

func TestAuthenticateRole(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLoginRepo := loginRepository.NewMockLoginRepo(ctrl)
mockLoginService := NewMockLoginService(ctrl)
impl := &LoginServiceImpl{mockLoginRepo, zap.NewNop().Sugar()}

// Set up expectations for AuthenticateRole
mockCookie := &http.Cookie{Value: "mocked_cookie_value"}
mockCheck := "teacher"

// Mock the NewJWT method
mockLoginService.EXPECT().NewJWT(gomock.Any(), gomock.Any()).Return(loginModels.JWT{}).AnyTimes()

// Mock the Validate method
mockLoginRepo.EXPECT().CheckRole(gomock.Any()).Return(loginModels.Teacher)

// Call the method in your service
result := impl.AuthenticateRole(mockCookie, mockCheck)

// Assert that the result is true as expected
assert.True(t, result)
}

func TestLogin(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLoginRepo := loginRepository.NewMockLoginRepo(ctrl)
mockLoginService := NewMockLoginService(ctrl)
impl := &LoginServiceImpl{mockLoginRepo, zap.NewNop().Sugar()}

// Set up expectations for Login
mockUsername := "test_user"
mockPassword := "test_password"
mockUserRole := "student"

// Mock the CheckCreds method
mockLoginRepo.EXPECT().CheckCreds(mockUsername, mockUserRole).Return("user_id", nil)

// Mock the AuthenticateUser method
mockLoginRepo.EXPECT().AuthenticateUser(mockUsername, mockPassword, mockUserRole).Return(true)

// Mock the NewJWT method
mockLoginService.EXPECT().NewJWT(gomock.Any(), gomock.Any()).Return(loginModels.JWT{}).AnyTimes()

// Call the method in your service
token := impl.Login(mockUsername, mockPassword, mockUserRole)

// Assert that the returned token is not nil
assert.NotNil(t, token)

}
1 change: 0 additions & 1 deletion pkg/login/loginServices/login_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions pkg/login/loginServices/mock_login_services.go

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

Loading

0 comments on commit eb3a67b

Please sign in to comment.