2021-04-29 01:49:24 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2023-06-26 13:23:00 +08:00
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-04-29 01:49:24 +08:00
|
|
|
"testing"
|
|
|
|
|
2023-09-01 16:57:08 +08:00
|
|
|
"github.com/go-jose/go-jose/v3/json"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2023-02-16 18:11:30 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-10-22 08:32:23 +08:00
|
|
|
"github.com/gravitl/netmaker/logic"
|
2021-04-29 01:49:24 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
)
|
|
|
|
|
2023-02-16 18:11:30 +08:00
|
|
|
func deleteAllUsers(t *testing.T) {
|
|
|
|
t.Helper()
|
2021-10-22 08:32:23 +08:00
|
|
|
users, _ := logic.GetUsers()
|
2021-09-06 22:36:14 +08:00
|
|
|
for _, user := range users {
|
2023-02-16 18:11:30 +08:00
|
|
|
if _, err := logic.DeleteUser(user.UserName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-10 03:46:31 +08:00
|
|
|
}
|
2021-04-29 01:49:24 +08:00
|
|
|
}
|
2021-09-06 22:36:14 +08:00
|
|
|
|
2023-06-26 13:23:00 +08:00
|
|
|
func TestGetUserNoHashedPassword(t *testing.T) {
|
|
|
|
// prepare existing user base
|
|
|
|
user := models.User{UserName: "freddie", Password: "password"}
|
|
|
|
haveOnlyOneUser(t, user)
|
|
|
|
|
|
|
|
// prepare request
|
|
|
|
rec, req := prepareUserRequest(t, models.User{}, user.UserName)
|
|
|
|
|
|
|
|
// test response
|
|
|
|
getUser(rec, req)
|
|
|
|
assertUserNameButNoPassword(t, rec.Body, user.UserName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAdminNoHashedPassword(t *testing.T) {
|
|
|
|
// prepare existing user base
|
|
|
|
deleteAllUsers(t)
|
|
|
|
|
|
|
|
// prepare request
|
|
|
|
user := models.User{UserName: "jonathan", Password: "password"}
|
|
|
|
rec, req := prepareUserRequest(t, user, "")
|
|
|
|
|
|
|
|
// test response
|
2023-09-01 16:57:08 +08:00
|
|
|
createSuperAdmin(rec, req)
|
2023-06-26 13:23:00 +08:00
|
|
|
assertUserNameButNoPassword(t, rec.Body, user.UserName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareUserRequest(t *testing.T, userForBody models.User, userNameForParam string) (*httptest.ResponseRecorder, *http.Request) {
|
|
|
|
bits, err := json.Marshal(userForBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
body := bytes.NewReader(bits)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("ANY", "https://example.com", body) // only the body matters here
|
|
|
|
req = mux.SetURLVars(req, map[string]string{"username": userNameForParam})
|
2023-09-01 16:57:08 +08:00
|
|
|
req.Header.Set("user", userForBody.UserName)
|
2023-06-26 13:23:00 +08:00
|
|
|
return rec, req
|
|
|
|
}
|
|
|
|
|
|
|
|
func haveOnlyOneUser(t *testing.T, user models.User) {
|
|
|
|
deleteAllUsers(t)
|
|
|
|
var err error
|
2023-09-01 16:57:08 +08:00
|
|
|
if user.IsSuperAdmin {
|
|
|
|
err = logic.CreateSuperAdmin(&user)
|
2023-06-26 13:23:00 +08:00
|
|
|
} else {
|
|
|
|
err = logic.CreateUser(&user)
|
|
|
|
}
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertUserNameButNoPassword(t *testing.T, r io.Reader, userName string) {
|
|
|
|
var resp models.User
|
|
|
|
err := json.NewDecoder(r).Decode(&resp)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, userName, resp.UserName)
|
|
|
|
assert.Empty(t, resp.Password)
|
|
|
|
}
|
|
|
|
|
2023-09-01 16:57:08 +08:00
|
|
|
func TestHasSuperAdmin(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
// delete all current users
|
2021-10-22 08:32:23 +08:00
|
|
|
users, _ := logic.GetUsers()
|
2021-09-06 22:36:14 +08:00
|
|
|
for _, user := range users {
|
2021-10-22 08:32:23 +08:00
|
|
|
success, err := logic.DeleteUser(user.UserName)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, success)
|
|
|
|
}
|
|
|
|
t.Run("NoUser", func(t *testing.T) {
|
2023-09-01 16:57:08 +08:00
|
|
|
found, err := logic.HasSuperAdmin()
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.False(t, found)
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
2023-09-01 16:57:08 +08:00
|
|
|
t.Run("No superadmin user", func(t *testing.T) {
|
|
|
|
var user = models.User{UserName: "nosuperadmin", Password: "password"}
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.CreateUser(&user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2023-09-01 16:57:08 +08:00
|
|
|
found, err := logic.HasSuperAdmin()
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.False(t, found)
|
|
|
|
})
|
2023-09-01 16:57:08 +08:00
|
|
|
t.Run("superadmin user", func(t *testing.T) {
|
|
|
|
var user = models.User{UserName: "superadmin", Password: "password", IsSuperAdmin: true}
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.CreateUser(&user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.Nil(t, err)
|
2023-09-01 16:57:08 +08:00
|
|
|
found, err := logic.HasSuperAdmin()
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, found)
|
|
|
|
})
|
2023-09-01 16:57:08 +08:00
|
|
|
t.Run("multiple superadmins", func(t *testing.T) {
|
|
|
|
var user = models.User{UserName: "superadmin1", Password: "password", IsSuperAdmin: true}
|
2023-02-14 20:01:38 +08:00
|
|
|
err := logic.CreateUser(&user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.Nil(t, err)
|
2023-09-01 16:57:08 +08:00
|
|
|
found, err := logic.HasSuperAdmin()
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, found)
|
|
|
|
})
|
2021-04-30 06:14:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUser(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
|
|
|
user := models.User{UserName: "admin", Password: "password", IsAdmin: true}
|
2021-04-30 06:14:13 +08:00
|
|
|
t.Run("NoUser", func(t *testing.T) {
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.CreateUser(&user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("UserExists", func(t *testing.T) {
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.CreateUser(&user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.NotNil(t, err)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "user exists")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-01 16:57:08 +08:00
|
|
|
func TestCreateSuperAdmin(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
2021-10-15 04:36:14 +08:00
|
|
|
var user models.User
|
2023-09-01 16:57:08 +08:00
|
|
|
t.Run("NoSuperAdmin", func(t *testing.T) {
|
2021-10-15 04:36:14 +08:00
|
|
|
user.UserName = "admin"
|
|
|
|
user.Password = "password"
|
2023-09-01 16:57:08 +08:00
|
|
|
err := logic.CreateSuperAdmin(&user)
|
2021-10-15 04:36:14 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
2023-09-01 16:57:08 +08:00
|
|
|
t.Run("SuperAdminExists", func(t *testing.T) {
|
2021-10-15 04:36:14 +08:00
|
|
|
user.UserName = "admin2"
|
|
|
|
user.Password = "password1"
|
2023-09-01 16:57:08 +08:00
|
|
|
err := logic.CreateSuperAdmin(&user)
|
|
|
|
assert.EqualError(t, err, "superadmin user already exists")
|
2021-10-15 04:36:14 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-29 01:49:24 +08:00
|
|
|
func TestDeleteUser(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("NonExistent User", func(t *testing.T) {
|
2021-10-22 08:32:23 +08:00
|
|
|
deleted, err := logic.DeleteUser("admin")
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "user does not exist")
|
|
|
|
assert.False(t, deleted)
|
2021-04-29 01:49:24 +08:00
|
|
|
})
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("Existing User", func(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
user := models.User{UserName: "admin", Password: "password", IsAdmin: true}
|
|
|
|
if err := logic.CreateUser(&user); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-10-22 08:32:23 +08:00
|
|
|
deleted, err := logic.DeleteUser("admin")
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.True(t, deleted)
|
2021-04-29 01:49:24 +08:00
|
|
|
})
|
|
|
|
}
|
2021-04-30 06:14:13 +08:00
|
|
|
|
|
|
|
func TestValidateUser(t *testing.T) {
|
|
|
|
var user models.User
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("Valid Create", func(t *testing.T) {
|
2021-04-30 06:14:13 +08:00
|
|
|
user.UserName = "admin"
|
|
|
|
user.Password = "validpass"
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("Valid Update", func(t *testing.T) {
|
2021-04-30 06:14:13 +08:00
|
|
|
user.UserName = "admin"
|
2021-04-30 23:30:19 +08:00
|
|
|
user.Password = "password"
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("Invalid UserName", func(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
user.UserName = "*invalid"
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.Error(t, err)
|
2023-02-16 18:11:30 +08:00
|
|
|
// assert.Contains(t, err.Error(), "Field validation for 'UserName' failed")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("Short UserName", func(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
user.UserName = "1"
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.NotNil(t, err)
|
2023-02-16 18:11:30 +08:00
|
|
|
// assert.Contains(t, err.Error(), "Field validation for 'UserName' failed")
|
2021-09-06 22:36:14 +08:00
|
|
|
})
|
|
|
|
t.Run("Empty UserName", func(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
user.UserName = ""
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "some string")
|
2023-02-16 18:11:30 +08:00
|
|
|
// assert.Contains(t, err.Error(), "Field validation for 'UserName' failed")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("EmptyPassword", func(t *testing.T) {
|
|
|
|
user.Password = ""
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "Key: 'User.Password' Error:Field validation for 'Password' failed on the 'required' tag")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("ShortPassword", func(t *testing.T) {
|
|
|
|
user.Password = "123"
|
2022-12-21 04:10:40 +08:00
|
|
|
err := logic.ValidateUser(&user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "Key: 'User.Password' Error:Field validation for 'Password' failed on the 'min' tag")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUser(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
|
|
|
|
2023-09-01 16:57:08 +08:00
|
|
|
user := models.User{UserName: "admin", Password: "password", IsAdmin: true}
|
2023-02-16 18:11:30 +08:00
|
|
|
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("NonExistantUser", func(t *testing.T) {
|
2021-10-22 08:32:23 +08:00
|
|
|
admin, err := logic.GetUser("admin")
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "could not find any records")
|
|
|
|
assert.Equal(t, "", admin.UserName)
|
|
|
|
})
|
2021-04-30 06:14:13 +08:00
|
|
|
t.Run("UserExisits", func(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
if err := logic.CreateUser(&user); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-10-22 08:32:23 +08:00
|
|
|
admin, err := logic.GetUser("admin")
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.Equal(t, user.UserName, admin.UserName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-16 23:47:57 +08:00
|
|
|
func TestGetUsers(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
|
|
|
|
|
|
|
adminUser := models.User{UserName: "admin", Password: "password", IsAdmin: true}
|
|
|
|
user := models.User{UserName: "admin", Password: "password", IsAdmin: false}
|
|
|
|
|
2021-10-16 23:47:57 +08:00
|
|
|
t.Run("NonExistantUser", func(t *testing.T) {
|
2021-10-22 08:32:23 +08:00
|
|
|
admin, err := logic.GetUsers()
|
2021-10-16 23:47:57 +08:00
|
|
|
assert.EqualError(t, err, "could not find any records")
|
|
|
|
assert.Equal(t, []models.ReturnUser(nil), admin)
|
|
|
|
})
|
|
|
|
t.Run("UserExisits", func(t *testing.T) {
|
2023-02-25 23:54:45 +08:00
|
|
|
user.UserName = "anotheruser"
|
2023-02-16 18:11:30 +08:00
|
|
|
if err := logic.CreateUser(&adminUser); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-10-22 08:32:23 +08:00
|
|
|
admins, err := logic.GetUsers()
|
2021-10-16 23:47:57 +08:00
|
|
|
assert.Nil(t, err)
|
2023-02-16 18:11:30 +08:00
|
|
|
assert.Equal(t, adminUser.UserName, admins[0].UserName)
|
2021-10-16 23:47:57 +08:00
|
|
|
})
|
|
|
|
t.Run("MulipleUsers", func(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
if err := logic.CreateUser(&user); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-10-22 08:32:23 +08:00
|
|
|
admins, err := logic.GetUsers()
|
2021-10-16 23:47:57 +08:00
|
|
|
assert.Nil(t, err)
|
2021-10-20 23:23:44 +08:00
|
|
|
for _, u := range admins {
|
|
|
|
if u.UserName == "admin" {
|
2023-02-16 18:11:30 +08:00
|
|
|
assert.Equal(t, true, u.IsAdmin)
|
2021-10-20 23:23:44 +08:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, user.UserName, u.UserName)
|
2023-02-16 18:11:30 +08:00
|
|
|
assert.Equal(t, user.IsAdmin, u.IsAdmin)
|
2021-10-20 23:23:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-16 23:47:57 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-30 06:14:13 +08:00
|
|
|
func TestUpdateUser(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
|
|
|
user := models.User{UserName: "admin", Password: "password", IsAdmin: true}
|
2023-09-01 16:57:08 +08:00
|
|
|
newuser := models.User{UserName: "hello", Password: "world", IsAdmin: true}
|
2021-09-06 22:36:14 +08:00
|
|
|
t.Run("NonExistantUser", func(t *testing.T) {
|
2022-12-21 04:10:40 +08:00
|
|
|
admin, err := logic.UpdateUser(&newuser, &user)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "could not find any records")
|
|
|
|
assert.Equal(t, "", admin.UserName)
|
|
|
|
})
|
|
|
|
|
2021-10-20 23:23:44 +08:00
|
|
|
t.Run("UserExists", func(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
if err := logic.CreateUser(&user); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-12-21 04:10:40 +08:00
|
|
|
admin, err := logic.UpdateUser(&newuser, &user)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.Equal(t, newuser.UserName, admin.UserName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:46:55 +08:00
|
|
|
// func TestValidateUserToken(t *testing.T) {
|
|
|
|
// t.Run("EmptyToken", func(t *testing.T) {
|
|
|
|
// err := ValidateUserToken("", "", false)
|
|
|
|
// assert.NotNil(t, err)
|
|
|
|
// assert.Equal(t, "Missing Auth Token.", err.Error())
|
|
|
|
// })
|
|
|
|
// t.Run("InvalidToken", func(t *testing.T) {
|
|
|
|
// err := ValidateUserToken("Bearer: badtoken", "", false)
|
|
|
|
// assert.NotNil(t, err)
|
|
|
|
// assert.Equal(t, "Error Verifying Auth Token", err.Error())
|
|
|
|
// })
|
|
|
|
// t.Run("InvalidUser", func(t *testing.T) {
|
|
|
|
// t.Skip()
|
|
|
|
// err := ValidateUserToken("Bearer: secretkey", "baduser", false)
|
|
|
|
// assert.NotNil(t, err)
|
|
|
|
// assert.Equal(t, "Error Verifying Auth Token", err.Error())
|
|
|
|
// //need authorization
|
|
|
|
// })
|
|
|
|
// t.Run("ValidToken", func(t *testing.T) {
|
|
|
|
// err := ValidateUserToken("Bearer: secretkey", "", true)
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
// })
|
|
|
|
// }
|
2021-04-30 06:14:13 +08:00
|
|
|
|
|
|
|
func TestVerifyAuthRequest(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
deleteAllUsers(t)
|
2023-09-01 16:57:08 +08:00
|
|
|
user := models.User{UserName: "admin", Password: "password", IsSuperAdmin: false, IsAdmin: true}
|
2021-04-30 06:14:13 +08:00
|
|
|
var authRequest models.UserAuthParams
|
|
|
|
t.Run("EmptyUserName", func(t *testing.T) {
|
|
|
|
authRequest.UserName = ""
|
|
|
|
authRequest.Password = "Password"
|
2021-10-22 08:32:23 +08:00
|
|
|
jwt, err := logic.VerifyAuthRequest(authRequest)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.Equal(t, "", jwt)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "username can't be empty")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("EmptyPassword", func(t *testing.T) {
|
|
|
|
authRequest.UserName = "admin"
|
|
|
|
authRequest.Password = ""
|
2021-10-22 08:32:23 +08:00
|
|
|
jwt, err := logic.VerifyAuthRequest(authRequest)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.Equal(t, "", jwt)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.EqualError(t, err, "password can't be empty")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("NonExistantUser", func(t *testing.T) {
|
|
|
|
authRequest.UserName = "admin"
|
|
|
|
authRequest.Password = "password"
|
2021-10-22 08:32:23 +08:00
|
|
|
jwt, err := logic.VerifyAuthRequest(authRequest)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.Equal(t, "", jwt)
|
2023-09-01 16:57:08 +08:00
|
|
|
assert.EqualError(t, err, "incorrect credentials")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("Non-Admin", func(t *testing.T) {
|
2023-02-25 23:54:45 +08:00
|
|
|
user.IsAdmin = false
|
|
|
|
user.Password = "somepass"
|
|
|
|
user.UserName = "nonadmin"
|
2023-02-16 18:11:30 +08:00
|
|
|
if err := logic.CreateUser(&user); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
authRequest := models.UserAuthParams{UserName: "nonadmin", Password: "somepass"}
|
2021-10-22 08:32:23 +08:00
|
|
|
jwt, err := logic.VerifyAuthRequest(authRequest)
|
2021-09-06 22:36:14 +08:00
|
|
|
assert.NotNil(t, jwt)
|
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("WrongPassword", func(t *testing.T) {
|
2023-09-01 16:57:08 +08:00
|
|
|
user := models.User{UserName: "admin", Password: "password"}
|
2023-02-16 18:11:30 +08:00
|
|
|
if err := logic.CreateUser(&user); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
authRequest := models.UserAuthParams{UserName: "admin", Password: "badpass"}
|
2021-10-22 08:32:23 +08:00
|
|
|
jwt, err := logic.VerifyAuthRequest(authRequest)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.Equal(t, "", jwt)
|
2021-10-06 03:02:09 +08:00
|
|
|
assert.EqualError(t, err, "incorrect credentials")
|
2021-04-30 06:14:13 +08:00
|
|
|
})
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
2023-02-16 18:11:30 +08:00
|
|
|
authRequest := models.UserAuthParams{UserName: "admin", Password: "password"}
|
2021-10-22 08:32:23 +08:00
|
|
|
jwt, err := logic.VerifyAuthRequest(authRequest)
|
2021-05-01 11:07:25 +08:00
|
|
|
assert.Nil(t, err)
|
2021-04-30 06:14:13 +08:00
|
|
|
assert.NotNil(t, jwt)
|
|
|
|
})
|
|
|
|
}
|