woodpecker/server/store/datastore/users_test.go

256 lines
7.1 KiB
Go
Raw Normal View History

// Copyright 2022 Woodpecker Authors
2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00: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 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00: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.
package datastore
2015-09-30 01:21:17 +00:00
import (
"testing"
"github.com/franela/goblin"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
2015-09-30 01:21:17 +00:00
)
func TestUsers(t *testing.T) {
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))
defer closer()
2015-09-30 01:21:17 +00: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() {
_, err := store.engine.Exec("DELETE FROM users")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM repos")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM pipelines")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM steps")
g.Assert(err).IsNil()
2023-08-21 13:04:12 +00:00
_, err = store.engine.Exec("DELETE FROM orgs")
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
})
g.It("Should Update a User", func() {
user := model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
err1 := store.CreateUser(&user)
err2 := store.UpdateUser(&user)
getuser, err3 := store.GetUser(user.ID)
g.Assert(err1).IsNil()
g.Assert(err2).IsNil()
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(user.ID).Equal(getuser.ID)
})
g.It("Should Add a new User", func() {
user := model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
err := store.CreateUser(&user)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(user.ID != 0).IsTrue()
})
g.It("Should Get a User", func() {
user := &model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Token: "f0b461ca586c27872b43a0685cbc2847",
Secret: "976f22a5eef7caacb7e678d6c52f49b1",
Email: "foo@bar.com",
Avatar: "b9015b0857e16ac4d94a0ffd9a0b79c8",
}
g.Assert(store.CreateUser(user)).IsNil()
getuser, err := store.GetUser(user.ID)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00: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() {
user := &model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
g.Assert(store.CreateUser(user))
getuser, err := store.GetUserLogin(user.Login)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(user.ID).Equal(getuser.ID)
g.Assert(user.Login).Equal(getuser.Login)
})
g.It("Should Enforce Unique User Login", func() {
user1 := model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
user2 := model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
}
err1 := store.CreateUser(&user1)
err2 := store.CreateUser(&user2)
g.Assert(err1).IsNil()
g.Assert(err2 == nil).IsFalse()
2015-09-30 01:21:17 +00:00
})
g.It("Should Get a User List", func() {
user1 := model.User{
2015-09-30 01:21:17 +00:00
Login: "jane",
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
Hash: "A",
2015-09-30 01:21:17 +00:00
}
user2 := model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
g.Assert(store.CreateUser(&user1)).IsNil()
g.Assert(store.CreateUser(&user2)).IsNil()
users, err := store.GetUserList(&model.ListOptions{Page: 1, PerPage: 50})
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00: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() {
user1 := model.User{
2015-09-30 01:21:17 +00:00
Login: "jane",
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
Hash: "A",
2015-09-30 01:21:17 +00:00
}
user2 := model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
Hash: "B",
2015-09-30 01:21:17 +00:00
}
g.Assert(store.CreateUser(&user1)).IsNil()
g.Assert(store.CreateUser(&user2)).IsNil()
count, err := store.GetUserCount()
g.Assert(err).IsNil()
g.Assert(count).Equal(int64(2))
2015-09-30 01:21:17 +00:00
})
g.It("Should Get a User Count Zero", func() {
count, err := store.GetUserCount()
g.Assert(err).IsNil()
g.Assert(count).Equal(int64(0))
2015-09-30 01:21:17 +00:00
})
g.It("Should Del a User", func() {
user := &model.User{
2015-09-30 01:21:17 +00:00
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
g.Assert(store.CreateUser(user)).IsNil()
user, err1 := store.GetUser(user.ID)
g.Assert(err1).IsNil()
err2 := store.DeleteUser(user)
g.Assert(err2).IsNil()
_, err3 := store.GetUser(user.ID)
g.Assert(err3).IsNotNil()
2015-09-30 01:21:17 +00:00
})
g.It("Should get the Pipeline feed for a User", func() {
2017-07-15 16:51:02 +00:00
user := &model.User{
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
g.Assert(store.CreateUser(user)).IsNil()
2017-07-15 16:51:02 +00:00
2015-10-21 23:39:43 +00:00
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
IsActive: true,
ForgeRemoteID: "1",
2015-10-21 23:39:43 +00:00
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
IsActive: true,
ForgeRemoteID: "2",
2015-10-21 23:39:43 +00:00
}
repo3 := &model.Repo{
Owner: "octocat",
Name: "hello-world",
FullName: "octocat/hello-world",
IsActive: true,
ForgeRemoteID: "3",
2015-10-21 23:39:43 +00:00
}
g.Assert(store.CreateRepo(repo1)).IsNil()
g.Assert(store.CreateRepo(repo2)).IsNil()
g.Assert(store.CreateRepo(repo3)).IsNil()
2015-10-21 23:39:43 +00:00
for _, perm := range []*model.Perm{
{UserID: user.ID, Repo: repo1, Push: true, Admin: false},
{UserID: user.ID, Repo: repo2, Push: false, Admin: true},
} {
g.Assert(store.PermUpsert(perm)).IsNil()
}
2017-07-15 16:51:02 +00:00
pipeline1 := &model.Pipeline{
2015-10-21 23:39:43 +00:00
RepoID: repo1.ID,
Status: model.StatusFailure,
}
pipeline2 := &model.Pipeline{
2015-10-21 23:39:43 +00:00
RepoID: repo1.ID,
Status: model.StatusSuccess,
}
pipeline3 := &model.Pipeline{
2015-10-21 23:39:43 +00:00
RepoID: repo2.ID,
Status: model.StatusSuccess,
}
pipeline4 := &model.Pipeline{
2015-10-21 23:39:43 +00:00
RepoID: repo3.ID,
Status: model.StatusSuccess,
}
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-21 23:39:43 +00:00
pipelines, err := store.UserFeed(user)
g.Assert(err).IsNil()
g.Assert(len(pipelines)).Equal(3)
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-21 23:39:43 +00:00
})
2015-09-30 01:21:17 +00:00
})
}