harbor/dao/dao_test.go

725 lines
20 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
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
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
2016-02-26 11:54:14 +01:00
2016-02-24 11:16:16 +01:00
package dao
2016-02-01 12:59:10 +01:00
import (
"os"
"testing"
"time"
2016-03-28 09:34:41 +02:00
"github.com/vmware/harbor/utils/log"
2016-02-01 12:59:10 +01:00
"github.com/vmware/harbor/models"
"github.com/astaxie/beego/orm"
)
func execUpdate(o orm.Ormer, sql string, params interface{}) error {
p, err := o.Raw(sql).Prepare()
if err != nil {
return err
}
defer p.Close()
_, err = p.Exec(params)
if err != nil {
return err
}
return nil
}
func clearUp(username string) {
2016-03-28 09:34:41 +02:00
var err error
2016-02-01 12:59:10 +01:00
o := orm.NewOrm()
o.Begin()
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete pm
from project_member pm
join user u
on pm.user_id = u.user_id
where u.username = ?`, username)
if err != nil {
o.Rollback()
log.Error(err)
}
err = execUpdate(o, `delete pm
from project_member pm
join project p
on pm.project_id = p.project_id
where p.name = ?`, projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete al
from access_log al
join user u
on al.user_id = u.user_id
where u.username = ?`, username)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete al
from access_log al
join project p
on al.project_id = p.project_id
where p.name = ?`, projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete from project where name = ?`, projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
err = execUpdate(o, `delete from user where username = ?`, username)
2016-02-01 12:59:10 +01:00
if err != nil {
o.Rollback()
2016-03-28 09:34:41 +02:00
log.Error(err)
2016-02-01 12:59:10 +01:00
}
o.Commit()
}
2016-02-26 04:26:54 +01:00
const username string = "Tester01"
const projectName string = "test_project"
2016-05-03 10:54:01 +02:00
const repoTag string = "test1.1"
const repoTag2 string = "test1.2"
2016-02-26 04:26:54 +01:00
const SysAdmin int = 1
const projectAdmin int = 2
const developer int = 3
const guest int = 4
2016-02-01 12:59:10 +01:00
2016-02-26 04:26:54 +01:00
const publicityOn = 1
const publicityOff = 0
2016-02-01 12:59:10 +01:00
func TestMain(m *testing.M) {
2016-02-23 13:49:59 +01:00
dbHost := os.Getenv("DB_HOST")
if len(dbHost) == 0 {
log.Fatalf("environment variable DB_HOST is not set")
}
dbUser := os.Getenv("DB_USR")
if len(dbUser) == 0 {
log.Fatalf("environment variable DB_USR is not set")
}
dbPort := os.Getenv("DB_PORT")
if len(dbPort) == 0 {
2016-02-24 03:53:25 +01:00
log.Fatalf("environment variable DB_PORT is not set")
2016-02-23 13:49:59 +01:00
}
dbPassword := os.Getenv("DB_PWD")
2016-02-01 12:59:10 +01:00
2016-03-29 07:33:49 +02:00
log.Infof("DB_HOST: %s, DB_USR: %s, DB_PORT: %s, DB_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
2016-04-01 13:23:31 +02:00
os.Setenv("MYSQL_HOST", dbHost)
os.Setenv("MYSQL_PORT", dbPort)
2016-02-23 13:49:59 +01:00
os.Setenv("MYSQL_USR", dbUser)
os.Setenv("MYSQL_PWD", dbPassword)
os.Setenv("AUTH_MODE", "db_auth")
2016-02-24 11:16:16 +01:00
InitDB()
2016-02-26 04:26:54 +01:00
clearUp(username)
2016-02-23 13:49:59 +01:00
os.Exit(m.Run())
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestRegister(t *testing.T) {
2016-02-01 12:59:10 +01:00
user := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
Email: "tester01@vmware.com",
Password: "Abc12345",
Realname: "tester01",
Comment: "register",
}
2016-02-24 11:16:16 +01:00
_, err := Register(user)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in Register: %v", err)
2016-02-01 12:59:10 +01:00
}
//Check if user registered successfully.
queryUser := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
newUser, err := GetUser(queryUser)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if newUser.Username != username {
t.Errorf("Username does not match, expected: %s, actual: %s", username, newUser.Username)
2016-02-23 13:49:59 +01:00
}
if newUser.Email != "tester01@vmware.com" {
t.Errorf("Email does not match, expected: %s, actual: %s", "tester01@vmware.com", newUser.Email)
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestUserExists(t *testing.T) {
2016-02-01 12:59:10 +01:00
var exists bool
var err error
2016-02-26 04:26:54 +01:00
exists, err = UserExists(models.User{Username: username}, "username")
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in UserExists: %v", err)
}
if !exists {
2016-02-26 04:26:54 +01:00
t.Errorf("User %s was inserted but does not exist", username)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
exists, err = UserExists(models.User{Email: "tester01@vmware.com"}, "email")
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in UserExists: %v", err)
}
if !exists {
t.Errorf("User with email %s inserted but does not exist", "tester01@vmware.com")
}
2016-02-24 11:16:16 +01:00
exists, err = UserExists(models.User{Username: "NOTHERE"}, "username")
2016-02-23 13:49:59 +01:00
if err != nil {
t.Errorf("Error occurred in UserExists: %v", err)
}
if exists {
t.Errorf("User %s was not inserted but does exist", "NOTHERE")
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestLoginByUserName(t *testing.T) {
2016-02-01 12:59:10 +01:00
userQuery := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
Password: "Abc12345",
}
2016-04-15 07:17:32 +02:00
loginUser, err := LoginByDb(models.AuthModel{
Principal: userQuery.Username,
Password: userQuery.Password,
})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
if loginUser == nil {
2016-02-23 13:49:59 +01:00
t.Errorf("No found for user logined by username and password: %v", userQuery)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if loginUser.Username != username {
t.Errorf("User's username does not match after login, expected: %s, actual: %s", username, loginUser.Username)
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestLoginByEmail(t *testing.T) {
2016-02-01 12:59:10 +01:00
userQuery := models.User{
Email: "tester01@vmware.com",
Password: "Abc12345",
}
2016-04-15 07:17:32 +02:00
loginUser, err := LoginByDb(models.AuthModel{
Principal: userQuery.Email,
Password: userQuery.Password,
})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
if loginUser == nil {
2016-02-23 13:49:59 +01:00
t.Errorf("No found for user logined by email and password : %v", userQuery)
}
2016-02-26 04:26:54 +01:00
if loginUser.Username != username {
t.Errorf("User's username does not match after login, expected: %s, actual: %s", username, loginUser.Username)
2016-02-01 12:59:10 +01:00
}
}
var currentUser *models.User
2016-02-23 13:49:59 +01:00
func TestGetUser(t *testing.T) {
2016-02-01 12:59:10 +01:00
queryUser := models.User{
2016-02-26 04:26:54 +01:00
Username: username,
2016-02-01 12:59:10 +01:00
}
var err error
2016-02-24 11:16:16 +01:00
currentUser, err = GetUser(queryUser)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
2016-02-01 12:59:10 +01:00
}
if currentUser == nil {
2016-02-23 13:49:59 +01:00
t.Errorf("No user found queried by user query: %+v", queryUser)
}
if currentUser.Email != "tester01@vmware.com" {
t.Errorf("the user's email does not match, expected: tester01@vmware.com, actual: %s", currentUser.Email)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestListUsers(t *testing.T) {
2016-02-24 11:16:16 +01:00
users, err := ListUsers(models.User{})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ListUsers: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if len(users) != 1 {
t.Errorf("Expect one user in list, but the acutal length is %d, the list: %+v", len(users), users)
}
2016-02-26 04:26:54 +01:00
users2, err := ListUsers(models.User{Username: username})
2016-02-23 13:49:59 +01:00
if len(users2) != 1 {
t.Errorf("Expect one user in list, but the acutal length is %d, the list: %+v", len(users), users)
}
2016-02-26 04:26:54 +01:00
if users2[0].Username != username {
t.Errorf("The username in result list does not match, expected: %s, actual: %s", username, users2[0].Username)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestResetUserPassword(t *testing.T) {
2016-02-24 11:16:16 +01:00
uuid, err := GenerateRandomString()
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GenerateRandomString: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
err = UpdateUserResetUUID(models.User{ResetUUID: uuid, Email: currentUser.Email})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in UpdateUserResetUuid: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 03:15:01 +01:00
err = ResetUserPassword(models.User{UserID: currentUser.UserID, Password: "HarborTester12345", ResetUUID: uuid, Salt: currentUser.Salt})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ResetUserPassword: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "HarborTester12345"})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if loginedUser.Username != username {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestChangeUserPassword(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewHarborTester12345", Salt: currentUser.Salt})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ChangeUserPassword: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewHarborTester12345"})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in LoginByDb: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if loginedUser.Username != username {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
func TestChangeUserPasswordWithOldPassword(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewerHarborTester12345", Salt: currentUser.Salt}, "NewHarborTester12345")
if err != nil {
t.Errorf("Error occurred in ChangeUserPassword: %v", err)
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewerHarborTester12345"})
if err != nil {
t.Errorf("Error occurred in LoginByDb: %v", err)
}
2016-02-26 04:26:54 +01:00
if loginedUser.Username != username {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
}
}
func TestChangeUserPasswordWithIncorrectOldPassword(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NNewerHarborTester12345", Salt: currentUser.Salt}, "WrongNewerHarborTester12345")
if err == nil {
t.Errorf("Error does not occurred due to old password is incorrect.")
}
2016-02-24 11:16:16 +01:00
loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NNewerHarborTester12345"})
if err != nil {
t.Errorf("Error occurred in LoginByDb: %v", err)
}
if loginedUser != nil {
t.Errorf("The login user is not nil, acutal: %+v", loginedUser)
}
}
2016-02-23 13:49:59 +01:00
func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) {
2016-05-16 11:50:27 +02:00
projects, err := SearchProjects(currentUser.UserID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in QueryRelevantProjects: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if len(projects) != 1 {
t.Errorf("Expected only one project in DB, but actual: %d", len(projects))
}
if projects[0].Name != "library" {
t.Errorf("There name of the project does not match, expected: %s, actual: %s", "library", projects[0].Name)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestAddProject(t *testing.T) {
2016-02-01 12:59:10 +01:00
project := models.Project{
2016-02-26 03:15:01 +01:00
OwnerID: currentUser.UserID,
2016-02-26 04:26:54 +01:00
Name: projectName,
2016-02-01 12:59:10 +01:00
CreationTime: time.Now(),
OwnerName: currentUser.Username,
}
_, err := AddProject(project)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in AddProject: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
newProject, err := GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
2016-02-01 12:59:10 +01:00
}
if newProject == nil {
2016-02-26 04:26:54 +01:00
t.Errorf("No project found queried by project name: %v", projectName)
2016-02-01 12:59:10 +01:00
}
}
var currentProject *models.Project
2016-02-23 13:49:59 +01:00
func TestGetProject(t *testing.T) {
2016-02-01 12:59:10 +01:00
var err error
2016-02-26 04:26:54 +01:00
currentProject, err = GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
2016-02-01 12:59:10 +01:00
}
if currentProject == nil {
2016-02-26 04:26:54 +01:00
t.Errorf("No project found queried by project name: %v", projectName)
2016-02-23 13:49:59 +01:00
}
2016-02-26 04:26:54 +01:00
if currentProject.Name != projectName {
t.Errorf("Project name does not match, expected: %s, actual: %s", projectName, currentProject.Name)
2016-02-01 12:59:10 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestGetAccessLog(t *testing.T) {
2016-02-01 12:59:10 +01:00
queryAccessLog := models.AccessLog{
2016-02-26 03:15:01 +01:00
UserID: currentUser.UserID,
ProjectID: currentProject.ProjectID,
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
accessLogs, err := GetAccessLogs(queryAccessLog)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetAccessLog: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if len(accessLogs) != 1 {
t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogs))
}
2016-02-26 04:26:54 +01:00
if accessLogs[0].RepoName != projectName+"/" {
t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogs[0].RepoName)
2016-02-01 12:59:10 +01:00
}
}
2016-05-03 10:54:01 +02:00
func TestAddAccessLog(t *testing.T) {
var err error
var accessLogList []models.AccessLog
accessLog := models.AccessLog{
UserID: currentUser.UserID,
ProjectID: currentProject.ProjectID,
RepoName: currentProject.Name + "/",
RepoTag: repoTag,
GUID: "N/A",
Operation: "create",
OpTime: time.Now(),
}
err = AddAccessLog(accessLog)
if err != nil {
t.Errorf("Error occurred in AddAccessLog: %v", err)
}
accessLogList, err = GetAccessLogs(accessLog)
if err != nil {
t.Errorf("Error occurred in GetAccessLog: %v", err)
}
if len(accessLogList) != 1 {
t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogList))
}
if accessLogList[0].RepoName != projectName+"/" {
t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogList[0].RepoName)
}
if accessLogList[0].RepoTag != repoTag {
t.Errorf("The repo tag does not match, expected: %s, actual: %s", repoTag, accessLogList[0].RepoTag)
}
}
func TestAccessLog(t *testing.T) {
var err error
var accessLogList []models.AccessLog
accessLog := models.AccessLog{
UserID: currentUser.UserID,
ProjectID: currentProject.ProjectID,
RepoName: currentProject.Name + "/",
RepoTag: repoTag2,
Operation: "create",
}
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/", repoTag2, "create")
if err != nil {
t.Errorf("Error occurred in AccessLog: %v", err)
}
accessLogList, err = GetAccessLogs(accessLog)
if err != nil {
t.Errorf("Error occurred in GetAccessLog: %v", err)
}
if len(accessLogList) != 1 {
t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogList))
}
if accessLogList[0].RepoName != projectName+"/" {
t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogList[0].RepoName)
}
if accessLogList[0].RepoTag != repoTag2 {
t.Errorf("The repo tag does not match, expected: %s, actual: %s", repoTag2, accessLogList[0].RepoTag)
}
}
2016-02-23 13:49:59 +01:00
func TestProjectExists(t *testing.T) {
2016-02-01 12:59:10 +01:00
var exists bool
var err error
2016-02-26 03:15:01 +01:00
exists, err = ProjectExists(currentProject.ProjectID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ProjectExists: %v", err)
}
if !exists {
2016-02-26 03:15:01 +01:00
t.Errorf("The project with id: %d, does not exist", currentProject.ProjectID)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
exists, err = ProjectExists(currentProject.Name)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ProjectExists: %v", err)
}
if !exists {
t.Errorf("The project with name: %s, does not exist", currentProject.Name)
2016-02-01 12:59:10 +01:00
}
}
2016-02-24 11:16:16 +01:00
func TestGetProjectById(t *testing.T) {
2016-02-26 03:15:01 +01:00
id := currentProject.ProjectID
2016-02-26 04:26:54 +01:00
p, err := GetProjectByID(id)
2016-02-24 11:16:16 +01:00
if err != nil {
t.Errorf("Error in GetProjectById: %v, id: %d", err, id)
}
if p.Name != currentProject.Name {
t.Errorf("project name does not match, expected: %s, actual: %s", currentProject.Name, p.Name)
}
}
func TestGetUserByProject(t *testing.T) {
2016-02-26 03:15:01 +01:00
pid := currentProject.ProjectID
2016-02-24 11:16:16 +01:00
u1 := models.User{
Username: "%%Tester%%",
}
u2 := models.User{
Username: "nononono",
}
users, err := GetUserByProject(pid, u1)
if err != nil {
2016-02-26 07:24:44 +01:00
t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u1)
2016-02-24 11:16:16 +01:00
}
if len(users) != 1 {
t.Errorf("unexpected length of user list, expected: 1, the users list: %+v", users)
}
users, err = GetUserByProject(pid, u2)
if err != nil {
2016-02-26 07:24:44 +01:00
t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u2)
2016-02-24 11:16:16 +01:00
}
if len(users) != 0 {
t.Errorf("unexpected length of user list, expected: 0, the users list: %+v", users)
}
}
2016-02-23 13:49:59 +01:00
func TestToggleProjectPublicity(t *testing.T) {
2016-02-26 04:26:54 +01:00
err := ToggleProjectPublicity(currentProject.ProjectID, publicityOn)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
currentProject, err = GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
}
2016-02-26 04:26:54 +01:00
if currentProject.Public != publicityOn {
2016-02-26 03:15:01 +01:00
t.Errorf("project, id: %d, its publicity is not on", currentProject.ProjectID)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
err = ToggleProjectPublicity(currentProject.ProjectID, publicityOff)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
currentProject, err = GetProjectByName(projectName)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetProjectByName: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
if currentProject.Public != publicityOff {
2016-02-26 03:15:01 +01:00
t.Errorf("project, id: %d, its publicity is not off", currentProject.ProjectID)
2016-02-23 13:49:59 +01:00
}
2016-02-24 11:16:16 +01:00
}
func TestIsProjectPublic(t *testing.T) {
2016-02-26 04:26:54 +01:00
if isPublic := IsProjectPublic(projectName); isPublic {
2016-02-26 03:15:01 +01:00
t.Errorf("project, id: %d, its publicity is not false after turning off", currentProject.ProjectID)
2016-02-24 11:16:16 +01:00
}
}
func TestGetUserProjectRoles(t *testing.T) {
2016-03-29 06:09:27 +02:00
r, err := GetUserProjectRoles(currentUser.UserID, currentProject.ProjectID)
2016-02-24 11:16:16 +01:00
if err != nil {
2016-03-29 06:09:27 +02:00
t.Errorf("Error happened in GetUserProjectRole: %v, userID: %+v, project Id: %d", err, currentUser.UserID, currentProject.ProjectID)
2016-02-24 11:16:16 +01:00
}
2016-02-01 12:59:10 +01:00
//Get the size of current user project role.
2016-02-23 13:49:59 +01:00
if len(r) != 1 {
2016-02-26 03:15:01 +01:00
t.Errorf("The user, id: %d, should only have one role in project, id: %d, but actual: %d", currentUser.UserID, currentProject.ProjectID, len(r))
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
if r[0].Name != "projectAdmin" {
t.Errorf("the expected rolename is: projectAdmin, actual: %s", r[0].Name)
}
2016-02-01 12:59:10 +01:00
}
2016-02-23 13:49:59 +01:00
func TestProjectPermission(t *testing.T) {
2016-02-24 11:16:16 +01:00
roleCode, err := GetPermission(currentUser.Username, currentProject.Name)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetPermission: %v", err)
}
if roleCode != "MDRWS" {
t.Errorf("The expected role code is MDRWS,but actual: %s", roleCode)
2016-02-01 12:59:10 +01:00
}
}
2016-05-10 16:08:36 +02:00
func TestGetUserRelevantProjects(t *testing.T) {
2016-05-17 11:03:40 +02:00
projects, err := GetUserRelevantProjects(currentUser.UserID, "")
2016-02-01 12:59:10 +01:00
if err != nil {
2016-05-10 16:08:36 +02:00
t.Errorf("Error occurred in GetUserRelevantProjects: %v", err)
2016-02-23 13:49:59 +01:00
}
2016-05-16 12:08:50 +02:00
if len(projects) != 1 {
t.Errorf("Expected length of relevant projects is 1, but actual: %d, the projects: %+v", len(projects), projects)
2016-02-01 12:59:10 +01:00
}
2016-05-16 12:08:50 +02:00
if projects[0].Name != projectName {
2016-02-26 04:26:54 +01:00
t.Errorf("Expected project name in the list: %s, actual: %s", projectName, projects[1].Name)
2016-02-01 12:59:10 +01:00
}
}
2016-05-10 16:08:36 +02:00
func TestGetAllProjects(t *testing.T) {
2016-05-17 11:03:40 +02:00
projects, err := GetAllProjects("")
2016-05-10 16:08:36 +02:00
if err != nil {
t.Errorf("Error occurred in GetAllProjects: %v", err)
}
2016-05-10 16:11:07 +02:00
if len(projects) != 2 {
t.Errorf("Expected length of projects is 2, but actual: %d, the projects: %+v", len(projects), projects)
}
if projects[1].Name != projectName {
t.Errorf("Expected project name in the list: %s, actual: %s", projectName, projects[1].Name)
}
2016-05-10 16:08:36 +02:00
}
2016-05-19 08:40:09 +02:00
func TestGetPublicProjects(t *testing.T) {
projects, err := GetPublicProjects("")
if err != nil {
t.Errorf("Error occurred in getProjects: %v", err)
}
if len(projects) != 1 {
t.Errorf("Expected length of projects is 1, but actual: %d, the projects: %+v", len(projects), projects)
}
if projects[0].Name != "library" {
t.Errorf("Expected project name in the list: %s, actual: %s", "library", projects[0].Name)
}
}
2016-03-28 09:34:41 +02:00
func TestAddProjectMember(t *testing.T) {
2016-03-29 06:09:27 +02:00
err := AddProjectMember(currentProject.ProjectID, 1, models.DEVELOPER)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-03-28 09:34:41 +02:00
t.Errorf("Error occurred in AddProjectMember: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-03-29 06:09:27 +02:00
roles, err := GetUserProjectRoles(1, currentProject.ProjectID)
2016-03-28 09:34:41 +02:00
if err != nil {
t.Errorf("Error occurred in GetUserProjectRoles: %v", err)
}
2016-02-01 12:59:10 +01:00
2016-03-28 09:34:41 +02:00
flag := false
for _, role := range roles {
if role.Name == "developer" {
flag = true
break
}
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
if !flag {
t.Errorf("the user which ID is 1 does not have developer privileges")
2016-02-23 13:49:59 +01:00
}
2016-02-01 12:59:10 +01:00
}
2016-03-28 09:34:41 +02:00
func TestDeleteProjectMember(t *testing.T) {
err := DeleteProjectMember(currentProject.ProjectID, 1)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-03-28 09:34:41 +02:00
t.Errorf("Error occurred in DeleteProjectMember: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-03-29 06:09:27 +02:00
roles, err := GetUserProjectRoles(1, currentProject.ProjectID)
2016-03-28 09:34:41 +02:00
if err != nil {
t.Errorf("Error occurred in GetUserProjectRoles: %v", err)
}
if len(roles) != 0 {
t.Errorf("delete record failed from table project_member")
2016-02-01 12:59:10 +01:00
}
}
2016-02-24 11:16:16 +01:00
func TestToggleAdminRole(t *testing.T) {
err := ToggleUserAdminRole(*currentUser)
if err != nil {
t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser)
}
2016-02-26 03:15:01 +01:00
isAdmin, err := IsAdminRole(currentUser.UserID)
2016-02-24 11:16:16 +01:00
if err != nil {
2016-02-26 03:15:01 +01:00
t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
if !isAdmin {
2016-02-26 03:15:01 +01:00
t.Errorf("User is not admin after toggled, user id: %d", currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
err = ToggleUserAdminRole(*currentUser)
if err != nil {
t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser)
}
2016-02-26 03:15:01 +01:00
isAdmin, err = IsAdminRole(currentUser.UserID)
2016-02-24 11:16:16 +01:00
if err != nil {
2016-02-26 03:15:01 +01:00
t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
if isAdmin {
2016-02-26 03:15:01 +01:00
t.Errorf("User is still admin after toggled, user id: %d", currentUser.UserID)
2016-02-24 11:16:16 +01:00
}
}
2016-02-23 13:49:59 +01:00
func TestDeleteUser(t *testing.T) {
2016-02-26 03:15:01 +01:00
err := DeleteUser(currentUser.UserID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in DeleteUser: %v", err)
2016-02-01 12:59:10 +01:00
}
2016-02-24 11:16:16 +01:00
user, err := GetUser(*currentUser)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-02-23 13:49:59 +01:00
t.Errorf("Error occurred in GetUser: %v", err)
}
if user != nil {
2016-02-26 07:24:44 +01:00
t.Errorf("user is not nil after deletion, user: %+v", user)
2016-02-01 12:59:10 +01:00
}
}
2016-05-20 11:35:16 +02:00
func TestGetOrmer(t *testing.T) {
o := GetOrmer()
if o == nil {
t.Errorf("Error get ormer.")
}
}