Skip to content

Commit

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

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/golang-web-app/internal/models"
"github.com/golang-web-app/internal/utils"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
)

func TestGenerateToken(t *testing.T) {
err := godotenv.Load("../../.env")
assert.NoError(t, err)

token, err := utils.GenerateToken(models.User{
Username: "testDev",
Email: "testdev@gmail.com",
Password: "Password",
})
assert.NoError(t, err)
assert.NotEmpty(t, token)
}

func TestValidateToken(t *testing.T) {
// Create a new HTTP request
req, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err)

// Set the token in the request header
req.Header.Set("Cookie", "this_is_a_jwt_fake_test_token")

// Create a ResponseRecorder (which implements http.ResponseWriter) to record the response
rr := httptest.NewRecorder()

r := gin.New()

// Define a route that calls ValidateToken
r.GET("/", func(c *gin.Context) {
user, err := utils.ValidateToken(c)
assert.Error(t, err)
assert.Nil(t, user)
})

// Perform the request
r.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
}

0 comments on commit dbc6f34

Please sign in to comment.