Skip to content

Commit

Permalink
unit test for hash and verify password
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdedman committed Apr 13, 2024
1 parent d32aa03 commit 1efe851
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/utils/encryption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils_test

import (
"testing"

"github.com/golang-web-app/internal/models"
"github.com/golang-web-app/internal/utils"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
)

// TestHashPassword tests the HashPassword function.
func TestHashPassword(t *testing.T) {
user := &models.User{
Password: "password123",
}

err := utils.HashPassword(user)
assert.NoError(t, err)

err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte("password123"))
assert.NoError(t, err)
}

// TestVerifyPassword tests the VerifyPassword function.
func TestVerifyPassword(t *testing.T) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
assert.NoError(t, err)

err = utils.VerifyPassword("password123", string(hashedPassword))
assert.NoError(t, err)
}

0 comments on commit 1efe851

Please sign in to comment.