2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/franela/goblin"
|
2021-10-12 09:25:13 +02:00
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func TestUsers(t *testing.T) {
|
2023-09-02 15:00:13 +02:00
|
|
|
store, closer := newTestStore(t, new(model.User), new(model.Repo), new(model.Pipeline), new(model.Step), new(model.Perm), new(model.Org), new(model.Secret))
|
2021-11-13 21:18:06 +02:00
|
|
|
defer closer()
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("User", func() {
|
|
|
|
// before each test be sure to purge the package
|
|
|
|
// table data from the database.
|
|
|
|
g.BeforeEach(func() {
|
2021-11-13 21:18:06 +02:00
|
|
|
_, err := store.engine.Exec("DELETE FROM users")
|
|
|
|
g.Assert(err).IsNil()
|
|
|
|
_, err = store.engine.Exec("DELETE FROM repos")
|
|
|
|
g.Assert(err).IsNil()
|
2022-10-18 03:24:12 +02:00
|
|
|
_, err = store.engine.Exec("DELETE FROM pipelines")
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(err).IsNil()
|
2022-10-28 17:38:53 +02:00
|
|
|
_, err = store.engine.Exec("DELETE FROM steps")
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(err).IsNil()
|
2023-08-21 15:04:12 +02:00
|
|
|
_, err = store.engine.Exec("DELETE FROM orgs")
|
|
|
|
g.Assert(err).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Update a User", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
user := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
err1 := store.CreateUser(&user)
|
|
|
|
err2 := store.UpdateUser(&user)
|
|
|
|
getuser, err3 := store.GetUser(user.ID)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err1).IsNil()
|
|
|
|
g.Assert(err2).IsNil()
|
|
|
|
g.Assert(err3).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(user.ID).Equal(getuser.ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Add a new User", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
user := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
err := store.CreateUser(&user)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(user.ID != 0).IsTrue()
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User", func() {
|
2021-11-13 21:18:06 +02:00
|
|
|
user := &model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Token: "f0b461ca586c27872b43a0685cbc2847",
|
|
|
|
Secret: "976f22a5eef7caacb7e678d6c52f49b1",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Avatar: "b9015b0857e16ac4d94a0ffd9a0b79c8",
|
|
|
|
}
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateUser(user)).IsNil()
|
|
|
|
getuser, err := store.GetUser(user.ID)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(user.ID).Equal(getuser.ID)
|
|
|
|
g.Assert(user.Login).Equal(getuser.Login)
|
|
|
|
g.Assert(user.Token).Equal(getuser.Token)
|
|
|
|
g.Assert(user.Secret).Equal(getuser.Secret)
|
|
|
|
g.Assert(user.Email).Equal(getuser.Email)
|
|
|
|
g.Assert(user.Avatar).Equal(getuser.Avatar)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User By Login", func() {
|
2021-11-13 21:18:06 +02:00
|
|
|
user := &model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateUser(user))
|
|
|
|
getuser, err := store.GetUserLogin(user.Login)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(user.ID).Equal(getuser.ID)
|
|
|
|
g.Assert(user.Login).Equal(getuser.Login)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Enforce Unique User Login", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
user1 := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
user2 := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "ab20g0ddaf012c744e136da16aa21ad9",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
err1 := store.CreateUser(&user1)
|
|
|
|
err2 := store.CreateUser(&user2)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err1).IsNil()
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(err2 == nil).IsFalse()
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User List", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
user1 := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "jane",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "ab20g0ddaf012c744e136da16aa21ad9",
|
2021-11-13 21:18:06 +02:00
|
|
|
Hash: "A",
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
user2 := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateUser(&user1)).IsNil()
|
|
|
|
g.Assert(store.CreateUser(&user2)).IsNil()
|
2023-04-30 03:40:13 +02:00
|
|
|
users, err := store.GetUserList(&model.ListOptions{Page: 1, PerPage: 50})
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(len(users)).Equal(2)
|
|
|
|
g.Assert(users[0].Login).Equal(user1.Login)
|
|
|
|
g.Assert(users[0].Email).Equal(user1.Email)
|
|
|
|
g.Assert(users[0].Token).Equal(user1.Token)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User Count", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
user1 := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "jane",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "ab20g0ddaf012c744e136da16aa21ad9",
|
2021-11-13 21:18:06 +02:00
|
|
|
Hash: "A",
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
user2 := model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
2021-11-13 21:18:06 +02:00
|
|
|
Hash: "B",
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateUser(&user1)).IsNil()
|
|
|
|
g.Assert(store.CreateUser(&user2)).IsNil()
|
|
|
|
count, err := store.GetUserCount()
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(count).Equal(int64(2))
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User Count Zero", func() {
|
2021-11-13 21:18:06 +02:00
|
|
|
count, err := store.GetUserCount()
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(count).Equal(int64(0))
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Del a User", func() {
|
2021-11-13 21:18:06 +02:00
|
|
|
user := &model.User{
|
2015-09-30 03:21:17 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateUser(user)).IsNil()
|
|
|
|
user, err1 := store.GetUser(user.ID)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err1).IsNil()
|
2021-11-13 21:18:06 +02:00
|
|
|
err2 := store.DeleteUser(user)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err2).IsNil()
|
2021-11-13 21:18:06 +02:00
|
|
|
_, err3 := store.GetUser(user.ID)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err3).IsNotNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
g.It("Should get the Pipeline feed for a User", func() {
|
2017-07-15 18:51:02 +02:00
|
|
|
user := &model.User{
|
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateUser(user)).IsNil()
|
2017-07-15 18:51:02 +02:00
|
|
|
|
2015-10-22 01:39:43 +02:00
|
|
|
repo1 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
IsActive: true,
|
|
|
|
ForgeRemoteID: "1",
|
2015-10-22 01:39:43 +02:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "test/test",
|
|
|
|
IsActive: true,
|
|
|
|
ForgeRemoteID: "2",
|
2015-10-22 01:39:43 +02:00
|
|
|
}
|
|
|
|
repo3 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "octocat",
|
|
|
|
Name: "hello-world",
|
|
|
|
FullName: "octocat/hello-world",
|
|
|
|
IsActive: true,
|
|
|
|
ForgeRemoteID: "3",
|
2015-10-22 01:39:43 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
g.Assert(store.CreateRepo(repo1)).IsNil()
|
|
|
|
g.Assert(store.CreateRepo(repo2)).IsNil()
|
|
|
|
g.Assert(store.CreateRepo(repo3)).IsNil()
|
2015-10-22 01:39:43 +02:00
|
|
|
|
2021-11-18 16:42:18 +02:00
|
|
|
for _, perm := range []*model.Perm{
|
2022-09-05 17:08:51 +02:00
|
|
|
{UserID: user.ID, Repo: repo1, Push: true, Admin: false},
|
|
|
|
{UserID: user.ID, Repo: repo2, Push: false, Admin: true},
|
2021-11-18 16:42:18 +02:00
|
|
|
} {
|
|
|
|
g.Assert(store.PermUpsert(perm)).IsNil()
|
|
|
|
}
|
2017-07-15 18:51:02 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pipeline1 := &model.Pipeline{
|
2015-10-22 01:39:43 +02:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusFailure,
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
pipeline2 := &model.Pipeline{
|
2015-10-22 01:39:43 +02:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusSuccess,
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
pipeline3 := &model.Pipeline{
|
2015-10-22 01:39:43 +02:00
|
|
|
RepoID: repo2.ID,
|
|
|
|
Status: model.StatusSuccess,
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
pipeline4 := &model.Pipeline{
|
2015-10-22 01:39:43 +02:00
|
|
|
RepoID: repo3.ID,
|
|
|
|
Status: model.StatusSuccess,
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
g.Assert(store.CreatePipeline(pipeline1)).IsNil()
|
|
|
|
g.Assert(store.CreatePipeline(pipeline2)).IsNil()
|
|
|
|
g.Assert(store.CreatePipeline(pipeline3)).IsNil()
|
|
|
|
g.Assert(store.CreatePipeline(pipeline4)).IsNil()
|
2015-10-22 01:39:43 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelines, err := store.UserFeed(user)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2022-10-18 03:24:12 +02:00
|
|
|
g.Assert(len(pipelines)).Equal(3)
|
2023-06-13 01:07:52 +02:00
|
|
|
g.Assert(pipelines[0].RepoID).Equal(repo2.ID)
|
|
|
|
g.Assert(pipelines[1].RepoID).Equal(repo1.ID)
|
|
|
|
g.Assert(pipelines[2].RepoID).Equal(repo1.ID)
|
2015-10-22 01:39:43 +02:00
|
|
|
})
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
}
|