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) {
|
2017-05-23 17:53:40 +02:00
|
|
|
s := newTest()
|
|
|
|
defer s.Close()
|
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() {
|
2017-05-23 17:53:40 +02:00
|
|
|
s.Exec("DELETE FROM users")
|
|
|
|
s.Exec("DELETE FROM repos")
|
|
|
|
s.Exec("DELETE FROM builds")
|
2017-07-15 18:51:02 +02:00
|
|
|
s.Exec("DELETE FROM procs")
|
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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
err1 := s.CreateUser(&user)
|
|
|
|
err2 := s.UpdateUser(&user)
|
|
|
|
getuser, err3 := s.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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
err := s.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() {
|
2015-10-22 01:14:02 +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",
|
|
|
|
Active: true,
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateUser(&user)
|
|
|
|
getuser, err := s.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.Assert(user.Active).Equal(getuser.Active)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User By Login", 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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateUser(&user)
|
|
|
|
getuser, err := s.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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
err1 := s.CreateUser(&user1)
|
|
|
|
err2 := s.CreateUser(&user2)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err1).IsNil()
|
|
|
|
g.Assert(err2).IsNotNil()
|
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",
|
|
|
|
}
|
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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateUser(&user1)
|
|
|
|
s.CreateUser(&user2)
|
|
|
|
users, err := s.GetUserList()
|
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",
|
|
|
|
}
|
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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateUser(&user1)
|
|
|
|
s.CreateUser(&user2)
|
|
|
|
count, err := s.GetUserCount()
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2017-05-23 17:53:40 +02:00
|
|
|
if s.driver != "postgres" {
|
|
|
|
// we have to skip this check for postgres because it uses
|
|
|
|
// an estimate which may not be updated.
|
|
|
|
g.Assert(count).Equal(2)
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a User Count Zero", func() {
|
2016-03-25 21:54:16 +02:00
|
|
|
count, err := s.GetUserCount()
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(count).Equal(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Del 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",
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateUser(&user)
|
|
|
|
_, err1 := s.GetUser(user.ID)
|
|
|
|
err2 := s.DeleteUser(&user)
|
|
|
|
_, err3 := s.GetUser(user.ID)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err1).IsNil()
|
|
|
|
g.Assert(err2).IsNil()
|
|
|
|
g.Assert(err3).IsNotNil()
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
2015-10-22 01:39:43 +02:00
|
|
|
g.It("Should get the Build feed for a User", func() {
|
2017-07-15 18:51:02 +02:00
|
|
|
user := &model.User{
|
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Token: "e42080dddf012c718e476da161d21ad5",
|
|
|
|
}
|
|
|
|
s.CreateUser(user)
|
|
|
|
|
2015-10-22 01:39:43 +02:00
|
|
|
repo1 := &model.Repo{
|
|
|
|
Owner: "bradrydzewski",
|
2021-10-02 10:59:34 +02:00
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
2017-07-15 18:51:02 +02:00
|
|
|
IsActive: true,
|
2015-10-22 01:39:43 +02:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2021-10-02 10:59:34 +02:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "test/test",
|
2017-07-15 18:51:02 +02:00
|
|
|
IsActive: true,
|
2015-10-22 01:39:43 +02:00
|
|
|
}
|
|
|
|
repo3 := &model.Repo{
|
|
|
|
Owner: "octocat",
|
|
|
|
Name: "hello-world",
|
|
|
|
FullName: "octocat/hello-world",
|
2017-07-15 18:51:02 +02:00
|
|
|
IsActive: true,
|
2015-10-22 01:39:43 +02:00
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateRepo(repo1)
|
|
|
|
s.CreateRepo(repo2)
|
|
|
|
s.CreateRepo(repo3)
|
2015-10-22 01:39:43 +02:00
|
|
|
|
2017-07-15 18:51:02 +02:00
|
|
|
s.PermBatch([]*model.Perm{
|
2021-10-26 21:29:30 +02:00
|
|
|
{UserID: user.ID, Repo: repo1.FullName, Push: true, Admin: false},
|
|
|
|
{UserID: user.ID, Repo: repo2.FullName, Push: false, Admin: true},
|
2017-07-15 18:51:02 +02:00
|
|
|
})
|
|
|
|
|
2015-10-22 01:39:43 +02:00
|
|
|
build1 := &model.Build{
|
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusFailure,
|
|
|
|
}
|
|
|
|
build2 := &model.Build{
|
|
|
|
RepoID: repo1.ID,
|
|
|
|
Status: model.StatusSuccess,
|
|
|
|
}
|
|
|
|
build3 := &model.Build{
|
|
|
|
RepoID: repo2.ID,
|
|
|
|
Status: model.StatusSuccess,
|
|
|
|
}
|
|
|
|
build4 := &model.Build{
|
|
|
|
RepoID: repo3.ID,
|
|
|
|
Status: model.StatusSuccess,
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
s.CreateBuild(build1)
|
|
|
|
s.CreateBuild(build2)
|
|
|
|
s.CreateBuild(build3)
|
|
|
|
s.CreateBuild(build4)
|
2015-10-22 01:39:43 +02:00
|
|
|
|
2017-07-15 18:51:02 +02:00
|
|
|
builds, err := s.UserFeed(user)
|
2021-11-04 15:42:25 +02:00
|
|
|
g.Assert(err).IsNil()
|
2015-10-22 01:39:43 +02:00
|
|
|
g.Assert(len(builds)).Equal(3)
|
|
|
|
g.Assert(builds[0].FullName).Equal(repo2.FullName)
|
|
|
|
g.Assert(builds[1].FullName).Equal(repo1.FullName)
|
|
|
|
g.Assert(builds[2].FullName).Equal(repo1.FullName)
|
|
|
|
})
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
}
|