Skip to content

Commit

Permalink
test: auth: init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hayzamjs committed Sep 7, 2024
1 parent 902f5c2 commit 70ddc8c
Show file tree
Hide file tree
Showing 5 changed files with 503 additions and 75 deletions.
108 changes: 54 additions & 54 deletions services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,60 @@ func NewAuthService(db *gorm.DB) *AuthService {
return &AuthService{DB: db}
}

func (service *AuthService) GetUsers() ([]models.User, error) {
var users []models.User

if err := service.DB.Find(&users).Error; err != nil {
return nil, fmt.Errorf("failed_to_get_users")
}

return users, nil
}

func (service *AuthService) CreateJWT(username, password string) (map[string]interface{}, error) {
var user models.User

if err := service.DB.Where("username = ?", username).First(&user).Error; err != nil {
return nil, fmt.Errorf("user_not_found")
}

if !utils.CheckPasswordHash(password, user.Password) {
return nil, fmt.Errorf("invalid_password")
}

tokenString, expiry, err := utils.GenerateJWTAccessToken(user.ID, user.Username, user.Email, user.Photo, user.Admin, user.Permissions)
if err != nil {
return nil, fmt.Errorf("failed_to_generate_jwt")
}

newToken := models.Token{
UserID: user.ID,
Token: tokenString,
Expiry: expiry,
}

if err := service.DB.Create(&newToken).Error; err != nil {
return nil, fmt.Errorf("failed_to_create_token")
}

claims, err := utils.ValidateJWT(tokenString)
if err != nil {
service.DB.Where("token = ?", tokenString).Delete(&models.Token{})
return nil, fmt.Errorf("invalid_jwt_created")
}

return map[string]interface{}{
"token": tokenString,
"expiry": claims.ExpiresAt.Time.String(),
"email": claims.Email,
"username": claims.Username,
"photo": claims.Photo,
"userId": claims.UserId,
"admin": user.Admin,
"permissions": claims.Permissions,
}, nil
}

func (service *AuthService) VerifyTokenInDb(token string, needAdmin bool) bool {
var tokenRecord models.Token

Expand Down Expand Up @@ -191,16 +245,6 @@ func (service *AuthService) DeleteUser(username string) error {
return nil
}

func (service *AuthService) GetUsers() ([]models.User, error) {
var users []models.User

if err := service.DB.Find(&users).Error; err != nil {
return nil, fmt.Errorf("failed_to_get_users")
}

return users, nil
}

func (service *AuthService) GetUser(id uint) (models.User, error) {
var user models.User

Expand All @@ -211,50 +255,6 @@ func (service *AuthService) GetUser(id uint) (models.User, error) {
return user, nil
}

func (service *AuthService) CreateJWT(username, password string) (map[string]interface{}, error) {
var user models.User

if err := service.DB.Where("username = ?", username).First(&user).Error; err != nil {
return nil, fmt.Errorf("user_not_found")
}

if !utils.CheckPasswordHash(password, user.Password) {
return nil, fmt.Errorf("invalid_password")
}

tokenString, expiry, err := utils.GenerateJWTAccessToken(user.ID, user.Username, user.Email, user.Photo, user.Admin, user.Permissions)
if err != nil {
return nil, fmt.Errorf("failed_to_generate_jwt")
}

newToken := models.Token{
UserID: user.ID,
Token: tokenString,
Expiry: expiry,
}

if err := service.DB.Create(&newToken).Error; err != nil {
return nil, fmt.Errorf("failed_to_create_token")
}

claims, err := utils.ValidateJWT(tokenString)
if err != nil {
service.DB.Where("token = ?", tokenString).Delete(&models.Token{})
return nil, fmt.Errorf("invalid_jwt_created")
}

return map[string]interface{}{
"token": tokenString,
"expiry": claims.ExpiresAt.Time.String(),
"email": claims.Email,
"username": claims.Username,
"photo": claims.Photo,
"userId": claims.UserId,
"admin": user.Admin,
"permissions": claims.Permissions,
}, nil
}

func (service *AuthService) CreateJWTFromEmail(email string) (string, error) {
var user models.User

Expand Down
Loading

0 comments on commit 70ddc8c

Please sign in to comment.