gpaste/auth_test.go
Torjus Håkestad a8a64d118c
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add auth middleware
2022-01-20 01:04:44 +01:00

40 lines
1.1 KiB
Go

package gpaste_test
import (
"testing"
"git.t-juice.club/torjus/gpaste"
)
func TestAuth(t *testing.T) {
t.Run("Token", func(t *testing.T) {
us := gpaste.NewMemoryUserStore()
secret := []byte(randomString(16))
as := gpaste.NewAuthService(us, secret)
username := randomString(8)
password := randomString(16)
user := &gpaste.User{Username: username}
if err := user.SetPassword(password); err != nil {
t.Fatalf("error setting user password: %s", err)
}
if err := us.Store(user); err != nil {
t.Fatalf("Error storing user: %s", err)
}
token, err := as.Login(username, password)
if err != nil {
t.Fatalf("Error creating token: %s", err)
}
if _, err := as.ValidateToken(token); err != nil {
t.Fatalf("Error validating token: %s", err)
}
invalidToken := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDMyMjk3NjMsImp0aSI6ImUzNDk5NWI1LThiZmMtNDQyNy1iZDgxLWFmNmQ3OTRiYzM0YiIsImlhdCI6MTY0MjYyNDk2MywibmJmIjoxNjQyNjI0OTYzLCJzdWIiOiJYdE5Hemt5ZSJ9.VM6dkwSLaBv8cStkWRVVv9ADjdUrHGHrlB7GB7Ly7n8`
if _, err := as.ValidateToken(invalidToken); err == nil {
t.Fatalf("Invalid token passed validation")
}
})
}