2021-05-14 13:47:32 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-05-16 23:44:42 +02:00
|
|
|
"time"
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
"github.com/semaphoreui/semaphore/db"
|
2024-11-23 12:31:38 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-05-14 13:47:32 +02:00
|
|
|
)
|
|
|
|
|
2021-05-16 23:44:42 +02:00
|
|
|
func TestBoltDb_UpdateProjectUser(t *testing.T) {
|
2022-01-23 14:21:27 +01:00
|
|
|
store := CreateTestStore()
|
2021-05-16 23:44:42 +02:00
|
|
|
|
|
|
|
usr, err := store.CreateUser(db.UserWithPwd{
|
|
|
|
Pwd: "123456",
|
|
|
|
User: db.User{
|
2022-01-23 14:21:27 +01:00
|
|
|
Email: "denguk@example.com",
|
|
|
|
Name: "Denis Gukov",
|
2021-05-16 23:44:42 +02:00
|
|
|
Username: "fiftin",
|
|
|
|
},
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-16 23:44:42 +02:00
|
|
|
|
|
|
|
proj1, err := store.CreateProject(db.Project{
|
|
|
|
Created: time.Now(),
|
2022-01-23 14:21:27 +01:00
|
|
|
Name: "Test1",
|
2021-05-16 23:44:42 +02:00
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-16 23:44:42 +02:00
|
|
|
|
|
|
|
projUser, err := store.CreateProjectUser(db.ProjectUser{
|
|
|
|
ProjectID: proj1.ID,
|
2022-01-23 14:21:27 +01:00
|
|
|
UserID: usr.ID,
|
2023-07-07 23:15:30 +02:00
|
|
|
Role: db.ProjectOwner,
|
2021-05-16 23:44:42 +02:00
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-16 23:44:42 +02:00
|
|
|
|
2023-07-07 23:15:30 +02:00
|
|
|
projUser.Role = db.ProjectOwner
|
2021-05-16 23:44:42 +02:00
|
|
|
err = store.UpdateProjectUser(projUser)
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-16 23:44:42 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 13:47:32 +02:00
|
|
|
func TestGetUsers(t *testing.T) {
|
2022-01-23 14:21:27 +01:00
|
|
|
store := CreateTestStore()
|
2021-05-14 13:47:32 +02:00
|
|
|
|
2022-01-23 14:21:27 +01:00
|
|
|
_, err := store.CreateUser(db.UserWithPwd{
|
2021-05-14 13:47:32 +02:00
|
|
|
Pwd: "123456",
|
|
|
|
User: db.User{
|
2022-01-23 14:21:27 +01:00
|
|
|
Email: "denguk@example.com",
|
|
|
|
Name: "Denis Gukov",
|
2021-05-14 13:47:32 +02:00
|
|
|
Username: "fiftin",
|
|
|
|
},
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-14 13:47:32 +02:00
|
|
|
|
|
|
|
found, err := store.GetUsers(db.RetrieveQueryParams{})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-14 13:47:32 +02:00
|
|
|
|
2024-11-23 12:31:38 +01:00
|
|
|
require.Equal(t, 1, len(found))
|
2021-05-14 13:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUser(t *testing.T) {
|
2022-01-23 14:21:27 +01:00
|
|
|
store := CreateTestStore()
|
2021-05-14 13:47:32 +02:00
|
|
|
|
|
|
|
usr, err := store.CreateUser(db.UserWithPwd{
|
|
|
|
Pwd: "123456",
|
|
|
|
User: db.User{
|
2022-01-23 14:21:27 +01:00
|
|
|
Email: "denguk@example.com",
|
|
|
|
Name: "Denis Gukov",
|
2021-05-14 13:47:32 +02:00
|
|
|
Username: "fiftin",
|
|
|
|
},
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-14 13:47:32 +02:00
|
|
|
|
|
|
|
found, err := store.GetUser(usr.ID)
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-14 13:47:32 +02:00
|
|
|
|
2024-11-23 12:31:38 +01:00
|
|
|
require.Equal(t, "fiftin", found.Username)
|
2021-05-14 13:47:32 +02:00
|
|
|
|
2021-05-14 13:57:51 +02:00
|
|
|
err = store.DeleteUser(usr.ID)
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-14 13:47:32 +02:00
|
|
|
}
|
2024-11-23 12:31:38 +01:00
|
|
|
|
2024-11-23 09:59:15 +01:00
|
|
|
func TestGetUserCount(t *testing.T) {
|
|
|
|
store := CreateTestStore()
|
|
|
|
|
|
|
|
// Create first user
|
|
|
|
_, err := store.CreateUser(db.UserWithPwd{
|
|
|
|
Pwd: "123456",
|
|
|
|
User: db.User{
|
|
|
|
Email: "user1@example.com",
|
|
|
|
Name: "User One",
|
|
|
|
Username: "userone",
|
|
|
|
},
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Create second user
|
|
|
|
_, err = store.CreateUser(db.UserWithPwd{
|
|
|
|
Pwd: "123456",
|
|
|
|
User: db.User{
|
|
|
|
Email: "user2@example.com",
|
|
|
|
Name: "User Two",
|
|
|
|
Username: "usertwo",
|
|
|
|
},
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Get user count
|
|
|
|
count, err := store.GetUserCount()
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Verify the count
|
2024-11-23 12:31:38 +01:00
|
|
|
require.Equal(t, 2, count)
|
2024-11-23 09:59:15 +01:00
|
|
|
}
|
2024-11-23 12:31:38 +01:00
|
|
|
|
2024-11-23 09:59:15 +01:00
|
|
|
func TestBoltDb_DeleteUser(t *testing.T) {
|
|
|
|
store := CreateTestStore()
|
|
|
|
|
|
|
|
// Create a user
|
|
|
|
usr, err := store.CreateUser(db.UserWithPwd{
|
|
|
|
Pwd: "123456",
|
|
|
|
User: db.User{
|
|
|
|
Email: "deleteuser@example.com",
|
|
|
|
Name: "Delete User",
|
|
|
|
Username: "deleteuser",
|
|
|
|
},
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Create a project
|
|
|
|
proj, err := store.CreateProject(db.Project{
|
|
|
|
Created: time.Now(),
|
|
|
|
Name: "DeleteUserProject",
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Associate the user with the project
|
|
|
|
_, err = store.CreateProjectUser(db.ProjectUser{
|
|
|
|
ProjectID: proj.ID,
|
|
|
|
UserID: usr.ID,
|
|
|
|
Role: db.ProjectOwner,
|
|
|
|
})
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Delete the user
|
|
|
|
err = store.DeleteUser(usr.ID)
|
2024-11-23 12:31:38 +01:00
|
|
|
require.NoError(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Verify the user is deleted
|
|
|
|
_, err = store.GetUser(usr.ID)
|
2024-11-23 12:31:38 +01:00
|
|
|
require.Error(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
|
|
|
|
// Verify the project-user association is deleted
|
|
|
|
_, err = store.GetProjectUser(proj.ID, usr.ID)
|
2024-11-23 12:31:38 +01:00
|
|
|
require.Error(t, err)
|
2024-11-23 09:59:15 +01:00
|
|
|
}
|