| 
									
										
										
										
											2016-02-01 19:59:10 +08: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 18:54:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | package dao | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/astaxie/beego/orm" | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	//"github.com/vmware/harbor/src/common/config"
 | 
					
						
							| 
									
										
										
										
											2016-10-19 14:32:00 +08:00
										 |  |  | 	"github.com/vmware/harbor/src/common/models" | 
					
						
							|  |  |  | 	"github.com/vmware/harbor/src/common/utils" | 
					
						
							|  |  |  | 	"github.com/vmware/harbor/src/common/utils/log" | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | func execUpdate(o orm.Ormer, sql string, params ...interface{}) error { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	p, err := o.Raw(sql).Prepare() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer p.Close() | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 	_, err = p.Exec(params...) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func clearUp(username string) { | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	o := GetOrmer() | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	o.Begin() | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	err = execUpdate(o, `delete  | 
					
						
							|  |  |  | 		from project_member  | 
					
						
							|  |  |  | 		where user_id = ( | 
					
						
							|  |  |  | 			select user_id | 
					
						
							|  |  |  | 			from user | 
					
						
							|  |  |  | 			where username = ? | 
					
						
							|  |  |  | 		) `, username) | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		o.Rollback() | 
					
						
							|  |  |  | 		log.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	err = execUpdate(o, `delete   | 
					
						
							|  |  |  | 		from project_member | 
					
						
							|  |  |  | 		where project_id = ( | 
					
						
							|  |  |  | 			select project_id | 
					
						
							|  |  |  | 			from project | 
					
						
							|  |  |  | 			where name = ? | 
					
						
							|  |  |  | 		)`, projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		o.Rollback() | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		log.Error(err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	err = execUpdate(o, `delete  | 
					
						
							|  |  |  | 		from access_log  | 
					
						
							|  |  |  | 		where user_id = ( | 
					
						
							|  |  |  | 			select user_id | 
					
						
							|  |  |  | 			from user | 
					
						
							|  |  |  | 			where username = ? | 
					
						
							|  |  |  | 		)`, username) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		o.Rollback() | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		log.Error(err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	err = execUpdate(o, `delete  | 
					
						
							|  |  |  | 		from access_log | 
					
						
							|  |  |  | 		where project_id = ( | 
					
						
							|  |  |  | 			select project_id | 
					
						
							|  |  |  | 			from project | 
					
						
							|  |  |  | 			where name = ? | 
					
						
							|  |  |  | 		)`, projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		o.Rollback() | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		log.Error(err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = execUpdate(o, `delete from project where name = ?`, projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		o.Rollback() | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		log.Error(err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = execUpdate(o, `delete from user where username = ?`, username) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		o.Rollback() | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		log.Error(err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = execUpdate(o, `delete from replication_job where id < 99`) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = execUpdate(o, `delete from replication_policy where id < 99`) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = execUpdate(o, `delete from replication_target where id < 99`) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	o.Commit() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | const username string = "Tester01" | 
					
						
							| 
									
										
										
										
											2016-08-05 16:23:38 +08:00
										 |  |  | const password string = "Abc12345" | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | const projectName string = "test_project" | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | const repositoryName string = "test_repository" | 
					
						
							| 
									
										
										
										
											2016-05-03 16:54:01 +08:00
										 |  |  | const repoTag string = "test1.1" | 
					
						
							|  |  |  | const repoTag2 string = "test1.2" | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | const SysAdmin int = 1 | 
					
						
							|  |  |  | const projectAdmin int = 2 | 
					
						
							|  |  |  | const developer int = 3 | 
					
						
							|  |  |  | const guest int = 4 | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | const publicityOn = 1 | 
					
						
							|  |  |  | const publicityOff = 0 | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMain(m *testing.M) { | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	databases := []string{"mysql", "sqlite"} | 
					
						
							|  |  |  | 	for _, database := range databases { | 
					
						
							|  |  |  | 		log.Infof("run test cases for database: %s", database) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		result := 1 | 
					
						
							|  |  |  | 		switch database { | 
					
						
							|  |  |  | 		case "mysql": | 
					
						
							|  |  |  | 			result = testForMySQL(m) | 
					
						
							|  |  |  | 		case "sqlite": | 
					
						
							|  |  |  | 			result = testForSQLite(m) | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			log.Fatalf("invalid database: %s", database) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if result != 0 { | 
					
						
							|  |  |  | 			os.Exit(result) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testForMySQL(m *testing.M) int { | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	dbHost := os.Getenv("MYSQL_HOST") | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	if len(dbHost) == 0 { | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 		log.Fatalf("environment variable MYSQL_HOST is not set") | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	dbUser := os.Getenv("MYSQL_USR") | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	if len(dbUser) == 0 { | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 		log.Fatalf("environment variable MYSQL_USR is not set") | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	dbPortStr := os.Getenv("MYSQL_PORT") | 
					
						
							|  |  |  | 	if len(dbPortStr) == 0 { | 
					
						
							|  |  |  | 		log.Fatalf("environment variable MYSQL_PORT is not set") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	dbPort, err := strconv.Atoi(dbPortStr) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Fatalf("invalid MYSQL_PORT: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	dbPassword := os.Getenv("MYSQL_PWD") | 
					
						
							|  |  |  | 	dbDatabase := os.Getenv("MYSQL_DATABASE") | 
					
						
							|  |  |  | 	if len(dbDatabase) == 0 { | 
					
						
							|  |  |  | 		log.Fatalf("environment variable MYSQL_DATABASE is not set") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	database := &models.Database{ | 
					
						
							|  |  |  | 		Type: "mysql", | 
					
						
							|  |  |  | 		MySQL: &models.MySQL{ | 
					
						
							|  |  |  | 			Host:     dbHost, | 
					
						
							|  |  |  | 			Port:     dbPort, | 
					
						
							|  |  |  | 			Username: dbUser, | 
					
						
							|  |  |  | 			Password: dbPassword, | 
					
						
							|  |  |  | 			Database: dbDatabase, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 21:36:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	log.Infof("MYSQL_HOST: %s, MYSQL_USR: %s, MYSQL_PORT: %s, MYSQL_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword) | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	return testForAll(m, database) | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testForSQLite(m *testing.M) int { | 
					
						
							|  |  |  | 	file := os.Getenv("SQLITE_FILE") | 
					
						
							|  |  |  | 	if len(file) == 0 { | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 		log.Fatalf("environment variable SQLITE_FILE is not set") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	database := &models.Database{ | 
					
						
							|  |  |  | 		Type: "sqlite", | 
					
						
							|  |  |  | 		SQLite: &models.SQLite{ | 
					
						
							|  |  |  | 			File: file, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 	return testForAll(m, database) | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | func testForAll(m *testing.M, database *models.Database) int { | 
					
						
							|  |  |  | 	initDatabaseForTest(database) | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	clearUp(username) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	return m.Run() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var defaultRegistered = false | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | func initDatabaseForTest(db *models.Database) { | 
					
						
							|  |  |  | 	database, err := getDatabase(db) | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log.Infof("initializing database: %s", database.String()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	alias := database.Name() | 
					
						
							|  |  |  | 	if !defaultRegistered { | 
					
						
							|  |  |  | 		defaultRegistered = true | 
					
						
							|  |  |  | 		alias = "default" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := database.Register(alias); err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-30 18:04:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if alias != "default" { | 
					
						
							|  |  |  | 		if err = globalOrm.Using(alias); err != nil { | 
					
						
							|  |  |  | 			log.Fatalf("failed to create new orm: %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestRegister(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	user := models.User{ | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		Username: username, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 		Email:    "tester01@vmware.com", | 
					
						
							| 
									
										
										
										
											2016-08-05 16:23:38 +08:00
										 |  |  | 		Password: password, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 		Realname: "tester01", | 
					
						
							|  |  |  | 		Comment:  "register", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	_, err := Register(user) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in Register: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//Check if user registered successfully.
 | 
					
						
							|  |  |  | 	queryUser := models.User{ | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		Username: username, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	newUser, err := GetUser(queryUser) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetUser: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	if newUser.Username != username { | 
					
						
							|  |  |  | 		t.Errorf("Username does not match, expected: %s, actual: %s", username, newUser.Username) | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 16:23:38 +08:00
										 |  |  | func TestCheckUserPassword(t *testing.T) { | 
					
						
							|  |  |  | 	nonExistUser := models.User{ | 
					
						
							|  |  |  | 		Username: "non-exist", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	correctUser := models.User{ | 
					
						
							|  |  |  | 		Username: username, | 
					
						
							|  |  |  | 		Password: password, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	wrongPwd := models.User{ | 
					
						
							|  |  |  | 		Username: username, | 
					
						
							|  |  |  | 		Password: "wrong", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	u, err := CheckUserPassword(nonExistUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed in CheckUserPassword: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if u != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected nil for Non exist user, but actual: %+v", u) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	u, err = CheckUserPassword(wrongPwd) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed in CheckUserPassword: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if u != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected nil for user with wrong password, but actual: %+v", u) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	u, err = CheckUserPassword(correctUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed in CheckUserPassword: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if u == nil { | 
					
						
							|  |  |  | 		t.Errorf("User should not be nil for correct user") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestUserExists(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	var exists bool | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	exists, err = UserExists(models.User{Username: username}, "username") | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in UserExists: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		t.Errorf("User %s was inserted but does not exist", username) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	exists, err = UserExists(models.User{Email: "tester01@vmware.com"}, "email") | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 18:16:16 +08:00
										 |  |  | 	exists, err = UserExists(models.User{Username: "NOTHERE"}, "username") | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestLoginByUserName(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	userQuery := models.User{ | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		Username: username, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 		Password: "Abc12345", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 13:17:32 +08:00
										 |  |  | 	loginUser, err := LoginByDb(models.AuthModel{ | 
					
						
							|  |  |  | 		Principal: userQuery.Username, | 
					
						
							|  |  |  | 		Password:  userQuery.Password, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in LoginByDb: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if loginUser == nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("No found for user logined by username and password: %v", userQuery) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08: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 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestLoginByEmail(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	userQuery := models.User{ | 
					
						
							|  |  |  | 		Email:    "tester01@vmware.com", | 
					
						
							|  |  |  | 		Password: "Abc12345", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 13:17:32 +08:00
										 |  |  | 	loginUser, err := LoginByDb(models.AuthModel{ | 
					
						
							|  |  |  | 		Principal: userQuery.Email, | 
					
						
							|  |  |  | 		Password:  userQuery.Password, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in LoginByDb: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if loginUser == nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("No found for user logined by email and password : %v", userQuery) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var currentUser *models.User | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestGetUser(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	queryUser := models.User{ | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		Username: username, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	currentUser, err = GetUser(queryUser) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetUser: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if currentUser == nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestListUsers(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	users, err := ListUsers(models.User{}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in ListUsers: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 11:26:54 +08:00
										 |  |  | 	users2, err := ListUsers(models.User{Username: username}) | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 11:26:54 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestResetUserPassword(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	uuid := utils.GenerateRandomString() | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 17:41:32 +08:00
										 |  |  | 	err := UpdateUserResetUUID(models.User{ResetUUID: uuid, Email: currentUser.Email}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in UpdateUserResetUuid: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 	err = ResetUserPassword(models.User{UserID: currentUser.UserID, Password: "HarborTester12345", ResetUUID: uuid, Salt: currentUser.Salt}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in ResetUserPassword: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "HarborTester12345"}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in LoginByDb: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08: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 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestChangeUserPassword(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-17 16:57:34 +08:00
										 |  |  | 	user := models.User{UserID: currentUser.UserID} | 
					
						
							|  |  |  | 	query, err := GetUser(user) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred when get user salt") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	currentUser.Salt = query.Salt | 
					
						
							|  |  |  | 	err = ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewHarborTester12345", Salt: currentUser.Salt}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in ChangeUserPassword: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewHarborTester12345"}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in LoginByDb: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08: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 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 20:44:46 +08:00
										 |  |  | func TestChangeUserPasswordWithOldPassword(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-17 16:57:34 +08:00
										 |  |  | 	user := models.User{UserID: currentUser.UserID} | 
					
						
							|  |  |  | 	query, err := GetUser(user) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred when get user salt") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	currentUser.Salt = query.Salt | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewerHarborTester12345", Salt: currentUser.Salt}, "NewHarborTester12345") | 
					
						
							| 
									
										
										
										
											2016-02-24 20:44:46 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in ChangeUserPassword: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewerHarborTester12345"}) | 
					
						
							| 
									
										
										
										
											2016-02-24 20:44:46 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in LoginByDb: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	if loginedUser.Username != username { | 
					
						
							|  |  |  | 		t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username) | 
					
						
							| 
									
										
										
										
											2016-02-24 20:44:46 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 21:36:45 +08:00
										 |  |  | func TestChangeUserPasswordWithIncorrectOldPassword(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 	err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NNewerHarborTester12345", Salt: currentUser.Salt}, "WrongNewerHarborTester12345") | 
					
						
							| 
									
										
										
										
											2016-02-24 21:36:45 +08:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Errorf("Error does not occurred due to old password is incorrect.") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NNewerHarborTester12345"}) | 
					
						
							| 
									
										
										
										
											2016-02-24 21:36:45 +08:00
										 |  |  | 	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 20:49:59 +08:00
										 |  |  | func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-05-16 17:50:27 +08:00
										 |  |  | 	projects, err := SearchProjects(currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in QueryRelevantProjects: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestAddProject(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	project := models.Project{ | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		OwnerID:      currentUser.UserID, | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		Name:         projectName, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 		CreationTime: time.Now(), | 
					
						
							|  |  |  | 		OwnerName:    currentUser.Username, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 17:57:39 +08:00
										 |  |  | 	_, err := AddProject(project) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in AddProject: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	newProject, err := GetProjectByName(projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetProjectByName: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if newProject == nil { | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		t.Errorf("No project found queried by project name: %v", projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var currentProject *models.Project | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestGetProject(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	currentProject, err = GetProjectByName(projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetProjectByName: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if currentProject == nil { | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		t.Errorf("No project found queried by project name: %v", projectName) | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	if currentProject.Name != projectName { | 
					
						
							|  |  |  | 		t.Errorf("Project name does not match, expected: %s, actual: %s", projectName, currentProject.Name) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestGetAccessLog(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	queryAccessLog := models.AccessLog{ | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		UserID:    currentUser.UserID, | 
					
						
							|  |  |  | 		ProjectID: currentProject.ProjectID, | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-02 18:34:35 +08:00
										 |  |  | 	accessLogs, err := GetAccessLogs(queryAccessLog, 1000, 0) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetAccessLog: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	if len(accessLogs) != 1 { | 
					
						
							|  |  |  | 		t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogs)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-02 18:34:35 +08:00
										 |  |  | func TestGetTotalOfAccessLogs(t *testing.T) { | 
					
						
							|  |  |  | 	queryAccessLog := models.AccessLog{ | 
					
						
							|  |  |  | 		UserID:    currentUser.UserID, | 
					
						
							|  |  |  | 		ProjectID: currentProject.ProjectID, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	total, err := GetTotalOfAccessLogs(queryAccessLog) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get total of access log: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if total != 1 { | 
					
						
							|  |  |  | 		t.Errorf("unexpected total %d != %d", total, 1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 16:54:01 +08: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) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-02 18:34:35 +08:00
										 |  |  | 	accessLogList, err = GetAccessLogs(accessLog, 1000, 0) | 
					
						
							| 
									
										
										
										
											2016-05-03 16:54:01 +08:00
										 |  |  | 	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) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-02 18:34:35 +08:00
										 |  |  | 	accessLogList, err = GetAccessLogs(accessLog, 1000, 0) | 
					
						
							| 
									
										
										
										
											2016-05-03 16:54:01 +08:00
										 |  |  | 	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-08-31 17:03:47 +08:00
										 |  |  | func TestGetAccessLogCreator(t *testing.T) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "push") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AccessLog: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "push") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AccessLog: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	user, err := GetAccessLogCreator(currentProject.Name + "/tomcat") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:38:06 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetAccessLogCreator: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if user != currentUser.Username { | 
					
						
							|  |  |  | 		t.Errorf("The access log creator does not match, expected: %s, actual: %s", currentUser.Username, user) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestCountPull(t *testing.T) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "pull") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AccessLog: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "pull") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AccessLog: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "pull") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AccessLog: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 18:11:51 +08:00
										 |  |  | 	pullCount, err := CountPull(currentProject.Name + "/tomcat") | 
					
						
							| 
									
										
										
										
											2016-08-31 17:38:06 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in CountPull: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if pullCount != 3 { | 
					
						
							|  |  |  | 		t.Errorf("The access log pull count does not match, expected: 3, actual: %d", pullCount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestProjectExists(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	var exists bool | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 	exists, err = ProjectExists(currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in ProjectExists: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("The project with id: %d, does not exist", currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	exists, err = ProjectExists(currentProject.Name) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | func TestGetProjectById(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 	id := currentProject.ProjectID | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	p, err := GetProjectByID(id) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08: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 10:15:01 +08:00
										 |  |  | 	pid := currentProject.ProjectID | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	u1 := models.User{ | 
					
						
							| 
									
										
										
										
											2016-11-16 20:25:40 +08:00
										 |  |  | 		Username: "Tester", | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	u2 := models.User{ | 
					
						
							|  |  |  | 		Username: "nononono", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	users, err := GetUserByProject(pid, u1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-26 14:24:44 +08:00
										 |  |  | 		t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u1) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08: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 14:24:44 +08:00
										 |  |  | 		t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u2) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if len(users) != 0 { | 
					
						
							|  |  |  | 		t.Errorf("unexpected length of user list, expected: 0, the users list: %+v", users) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestToggleProjectPublicity(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	err := ToggleProjectPublicity(currentProject.ProjectID, publicityOn) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in ToggleProjectPublicity: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	currentProject, err = GetProjectByName(projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetProjectByName: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	if currentProject.Public != publicityOn { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("project, id: %d, its publicity is not on", currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	err = ToggleProjectPublicity(currentProject.ProjectID, publicityOff) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in ToggleProjectPublicity: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	currentProject, err = GetProjectByName(projectName) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetProjectByName: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	if currentProject.Public != publicityOff { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("project, id: %d, its publicity is not off", currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestIsProjectPublic(t *testing.T) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 	if isPublic := IsProjectPublic(projectName); isPublic { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("project, id: %d, its publicity is not false after turning off", currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetUserProjectRoles(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-03-29 12:09:27 +08:00
										 |  |  | 	r, err := GetUserProjectRoles(currentUser.UserID, currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-29 12:09:27 +08:00
										 |  |  | 		t.Errorf("Error happened in GetUserProjectRole: %v, userID: %+v, project Id: %d", err, currentUser.UserID, currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//Get the size of current user project role.
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	if len(r) != 1 { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	if r[0].Name != "projectAdmin" { | 
					
						
							|  |  |  | 		t.Errorf("the expected rolename is: projectAdmin, actual: %s", r[0].Name) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | func TestProjectPermission(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	roleCode, err := GetPermission(currentUser.Username, currentProject.Name) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 16:35:48 +08:00
										 |  |  | func TestGetTotalOfUserRelevantProjects(t *testing.T) { | 
					
						
							|  |  |  | 	total, err := GetTotalOfUserRelevantProjects(currentUser.UserID, "") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get total of user relevant projects: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if total != 1 { | 
					
						
							|  |  |  | 		t.Errorf("unexpected total: %d != 1", total) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 22:08:36 +08:00
										 |  |  | func TestGetUserRelevantProjects(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-05-17 17:03:40 +08:00
										 |  |  | 	projects, err := GetUserRelevantProjects(currentUser.UserID, "") | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-05-10 22:08:36 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetUserRelevantProjects: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-16 18:08:50 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-16 18:08:50 +08:00
										 |  |  | 	if projects[0].Name != projectName { | 
					
						
							| 
									
										
										
										
											2016-02-26 11:26:54 +08:00
										 |  |  | 		t.Errorf("Expected project name in the list: %s, actual: %s", projectName, projects[1].Name) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 16:35:48 +08:00
										 |  |  | func TestGetTotalOfProjects(t *testing.T) { | 
					
						
							|  |  |  | 	total, err := GetTotalOfProjects("") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get total of projects: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if total != 2 { | 
					
						
							|  |  |  | 		t.Errorf("unexpected total: %d != 2", total) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetProjects(t *testing.T) { | 
					
						
							|  |  |  | 	projects, err := GetProjects("") | 
					
						
							| 
									
										
										
										
											2016-05-10 22:08:36 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetAllProjects: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-10 22:11:07 +08: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 22:08:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-19 14:40:09 +08:00
										 |  |  | func TestGetPublicProjects(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-08-10 16:35:48 +08:00
										 |  |  | 	projects, err := GetProjects("", 1) | 
					
						
							| 
									
										
										
										
											2016-05-19 14:40:09 +08:00
										 |  |  | 	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 15:34:41 +08:00
										 |  |  | func TestAddProjectMember(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-03-29 12:09:27 +08:00
										 |  |  | 	err := AddProjectMember(currentProject.ProjectID, 1, models.DEVELOPER) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		t.Errorf("Error occurred in AddProjectMember: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 12:09:27 +08:00
										 |  |  | 	roles, err := GetUserProjectRoles(1, currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetUserProjectRoles: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 	flag := false | 
					
						
							|  |  |  | 	for _, role := range roles { | 
					
						
							|  |  |  | 		if role.Name == "developer" { | 
					
						
							|  |  |  | 			flag = true | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 	if !flag { | 
					
						
							|  |  |  | 		t.Errorf("the user which ID is 1 does not have developer privileges") | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 16:23:38 +08:00
										 |  |  | func TestUpdateProjectMember(t *testing.T) { | 
					
						
							|  |  |  | 	err := UpdateProjectMember(currentProject.ProjectID, 1, models.GUEST) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in UpdateProjectMember: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	roles, err := GetUserProjectRoles(1, currentProject.ProjectID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetUserProjectRoles: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if roles[0].Name != "guest" { | 
					
						
							|  |  |  | 		t.Errorf("The user with ID 1 is not guest role after update, the acutal role: %s", roles[0].Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | func TestDeleteProjectMember(t *testing.T) { | 
					
						
							|  |  |  | 	err := DeleteProjectMember(currentProject.ProjectID, 1) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08:00
										 |  |  | 		t.Errorf("Error occurred in DeleteProjectMember: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 12:09:27 +08:00
										 |  |  | 	roles, err := GetUserProjectRoles(1, currentProject.ProjectID) | 
					
						
							| 
									
										
										
										
											2016-03-28 15:34:41 +08: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 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 16:23:38 +08:00
										 |  |  | func TestGetRoleByID(t *testing.T) { | 
					
						
							|  |  |  | 	r, err := GetRoleByID(models.PROJECTADMIN) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to call GetRoleByID: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if r == nil || r.Name != "projectAdmin" || r.RoleCode != "MDRWS" { | 
					
						
							|  |  |  | 		t.Errorf("Role does not match for role id: %d, actual: %+v", models.PROJECTADMIN, r) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	r, err = GetRoleByID(9999) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to call GetRoleByID: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if r != nil { | 
					
						
							|  |  |  | 		t.Errorf("Role should nil for non-exist id 9999, actual: %+v", r) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | func TestToggleAdminRole(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-06-02 19:03:04 +08:00
										 |  |  | 	err := ToggleUserAdminRole(currentUser.UserID, 1) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 	isAdmin, err := IsAdminRole(currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !isAdmin { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("User is not admin after toggled, user id: %d", currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-02 19:03:04 +08:00
										 |  |  | 	err = ToggleUserAdminRole(currentUser.UserID, 0) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 	isAdmin, err = IsAdminRole(currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if isAdmin { | 
					
						
							| 
									
										
										
										
											2016-02-26 10:15:01 +08:00
										 |  |  | 		t.Errorf("User is still admin after toggled, user id: %d", currentUser.UserID) | 
					
						
							| 
									
										
										
										
											2016-02-24 18:16:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 14:24:42 +08:00
										 |  |  | func TestChangeUserProfile(t *testing.T) { | 
					
						
							|  |  |  | 	user := models.User{UserID: currentUser.UserID, Email: username + "@163.com", Realname: "test", Comment: "Unit Test"} | 
					
						
							|  |  |  | 	err := ChangeUserProfile(user) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-05-23 14:24:42 +08:00
										 |  |  | 		t.Errorf("Error occurred in ChangeUserProfile: %v", err) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-23 14:24:42 +08:00
										 |  |  | 	loginedUser, err := GetUser(models.User{UserID: currentUser.UserID}) | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-02-23 20:49:59 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetUser: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-23 14:24:42 +08:00
										 |  |  | 	if loginedUser != nil { | 
					
						
							|  |  |  | 		if loginedUser.Email != username+"@163.com" { | 
					
						
							|  |  |  | 			t.Errorf("user email does not update, expected: %s, acutal: %s", username+"@163.com", loginedUser.Email) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if loginedUser.Realname != "test" { | 
					
						
							|  |  |  | 			t.Errorf("user realname does not update, expected: %s, acutal: %s", "test", loginedUser.Realname) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if loginedUser.Comment != "Unit Test" { | 
					
						
							|  |  |  | 			t.Errorf("user email does not update, expected: %s, acutal: %s", "Unit Test", loginedUser.Comment) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-01 19:59:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-20 16:56:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 17:22:38 +08:00
										 |  |  | func TestGetRecentLogs(t *testing.T) { | 
					
						
							|  |  |  | 	logs, err := GetRecentLogs(currentUser.UserID, 10, "2016-05-13 00:00:00", time.Now().String()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("error occured in getting recent logs, error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(logs) <= 0 { | 
					
						
							|  |  |  | 		t.Errorf("get logs error, expected: %d, actual: %d", 1, len(logs)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 14:26:41 +08:00
										 |  |  | func TestGetTopRepos(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-02 11:24:06 +08:00
										 |  |  | 	_, err := GetTopRepos(10) | 
					
						
							| 
									
										
										
										
											2016-06-27 14:26:41 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-09-02 11:24:06 +08:00
										 |  |  | 		t.Fatalf("error occured in getting top repos, error: %v", err) | 
					
						
							| 
									
										
										
										
											2016-06-27 14:26:41 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | var targetID, policyID, policyID2, policyID3, jobID, jobID2, jobID3 int64 | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestAddRepTarget(t *testing.T) { | 
					
						
							|  |  |  | 	target := models.RepTarget{ | 
					
						
							| 
									
										
										
										
											2016-06-13 08:42:11 +08:00
										 |  |  | 		Name:     "test", | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 		URL:      "127.0.0.1:5000", | 
					
						
							|  |  |  | 		Username: "admin", | 
					
						
							|  |  |  | 		Password: "admin", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	//_, err := AddRepTarget(target)
 | 
					
						
							|  |  |  | 	id, err := AddRepTarget(target) | 
					
						
							|  |  |  | 	t.Logf("added target, id: %d", id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AddRepTarget: %v", err) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		targetID = id | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	id2 := id + 99 | 
					
						
							|  |  |  | 	tgt, err := GetRepTarget(id2) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetTarget: %v, id: %d", err, id2) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if tgt != nil { | 
					
						
							|  |  |  | 		t.Errorf("There should not be a target with id: %d", id2) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	tgt, err = GetRepTarget(id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetTarget: %v, id: %d", err, id) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if tgt == nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to find a target with id: %d", id) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if tgt.URL != "127.0.0.1:5000" { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected url in target: %s, expected 127.0.0.1:5000", tgt.URL) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if tgt.Username != "admin" { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected username in target: %s, expected admin", tgt.Username) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-07 13:41:02 +08:00
										 |  |  | func TestGetRepTargetByName(t *testing.T) { | 
					
						
							|  |  |  | 	target, err := GetRepTarget(targetID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get target %d: %v", targetID, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	target2, err := GetRepTargetByName(target.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get target %s: %v", target.Name, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if target.Name != target2.Name { | 
					
						
							|  |  |  | 		t.Errorf("unexpected target name: %s, expected: %s", target2.Name, target.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 14:35:52 +08:00
										 |  |  | func TestGetRepTargetByEndpoint(t *testing.T) { | 
					
						
							|  |  |  | 	target, err := GetRepTarget(targetID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get target %d: %v", targetID, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	target2, err := GetRepTargetByEndpoint(target.URL) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get target %s: %v", target.URL, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if target.URL != target2.URL { | 
					
						
							|  |  |  | 		t.Errorf("unexpected target URL: %s, expected: %s", target2.URL, target.URL) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-07 13:41:02 +08:00
										 |  |  | func TestUpdateRepTarget(t *testing.T) { | 
					
						
							|  |  |  | 	target := &models.RepTarget{ | 
					
						
							|  |  |  | 		Name:     "name", | 
					
						
							|  |  |  | 		URL:      "http://url", | 
					
						
							|  |  |  | 		Username: "username", | 
					
						
							|  |  |  | 		Password: "password", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	id, err := AddRepTarget(*target) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to add target: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err := DeleteRepTarget(id); err != nil { | 
					
						
							|  |  |  | 			t.Logf("failed to delete target %d: %v", id, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	target.ID = id | 
					
						
							|  |  |  | 	target.Name = "new_name" | 
					
						
							|  |  |  | 	target.URL = "http://new_url" | 
					
						
							|  |  |  | 	target.Username = "new_username" | 
					
						
							|  |  |  | 	target.Password = "new_password" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = UpdateRepTarget(*target); err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to update target: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	target, err = GetRepTarget(id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get target %d: %v", id, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if target.Name != "new_name" { | 
					
						
							|  |  |  | 		t.Errorf("unexpected name: %s, expected: %s", target.Name, "new_name") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if target.URL != "http://new_url" { | 
					
						
							|  |  |  | 		t.Errorf("unexpected url: %s, expected: %s", target.URL, "http://new_url") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if target.Username != "new_username" { | 
					
						
							|  |  |  | 		t.Errorf("unexpected username: %s, expected: %s", target.Username, "new_username") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if target.Password != "new_password" { | 
					
						
							|  |  |  | 		t.Errorf("unexpected password: %s, expected: %s", target.Password, "new_password") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-13 08:42:11 +08:00
										 |  |  | func TestFilterRepTargets(t *testing.T) { | 
					
						
							|  |  |  | 	targets, err := FilterRepTargets("test") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get all targets: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(targets) == 0 { | 
					
						
							|  |  |  | 		t.Errorf("unexpected num of targets: %d, expected: %d", len(targets), 1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | func TestAddRepPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	policy := models.RepPolicy{ | 
					
						
							|  |  |  | 		ProjectID:   1, | 
					
						
							|  |  |  | 		Enabled:     1, | 
					
						
							|  |  |  | 		TargetID:    targetID, | 
					
						
							|  |  |  | 		Description: "whatever", | 
					
						
							|  |  |  | 		Name:        "mypolicy", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	id, err := AddRepPolicy(policy) | 
					
						
							|  |  |  | 	t.Logf("added policy, id: %d", id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AddRepPolicy: %v", err) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		policyID = id | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	p, err := GetRepPolicy(id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, id) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if p == nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to find a policy with id: %d", id) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if p.Name != "mypolicy" || p.TargetID != targetID || p.Enabled != 1 || p.Description != "whatever" { | 
					
						
							|  |  |  | 		t.Errorf("The data does not match, expected: Name: mypolicy, TargetID: %d, Enabled: 1, Description: whatever;\n result: Name: %s, TargetID: %d, Enabled: %d, Description: %s", | 
					
						
							|  |  |  | 			targetID, p.Name, p.TargetID, p.Enabled, p.Description) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-27 18:46:07 +08:00
										 |  |  | 	var tm = time.Now().AddDate(0, 0, -1) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	if !p.StartTime.After(tm) { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected start_time: %v", p.StartTime) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 14:18:23 +08:00
										 |  |  | func TestGetRepPolicyByTarget(t *testing.T) { | 
					
						
							|  |  |  | 	policies, err := GetRepPolicyByTarget(targetID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get policy according target %d: %v", targetID, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(policies) == 0 { | 
					
						
							|  |  |  | 		t.Fatal("unexpected length of policies 0, expected is >0") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if policies[0].ID != policyID { | 
					
						
							| 
									
										
										
										
											2016-06-16 14:24:35 +08:00
										 |  |  | 		t.Fatalf("unexpected policy: %d, expected: %d", policies[0].ID, policyID) | 
					
						
							| 
									
										
										
										
											2016-06-16 14:18:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:19:19 +08:00
										 |  |  | func TestGetRepPolicyByProjectAndTarget(t *testing.T) { | 
					
						
							|  |  |  | 	policies, err := GetRepPolicyByProjectAndTarget(1, targetID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get policy according project %d and target %d: %v", 1, targetID, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(policies) == 0 { | 
					
						
							|  |  |  | 		t.Fatal("unexpected length of policies 0, expected is >0") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if policies[0].ID != policyID { | 
					
						
							|  |  |  | 		t.Fatalf("unexpected policy: %d, expected: %d", policies[0].ID, policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-07 13:41:02 +08:00
										 |  |  | func TestGetRepPolicyByName(t *testing.T) { | 
					
						
							|  |  |  | 	policy, err := GetRepPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get policy %d: %v", policyID, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	policy2, err := GetRepPolicyByName(policy.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get policy %s: %v", policy.Name, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if policy.Name != policy2.Name { | 
					
						
							|  |  |  | 		t.Errorf("unexpected name: %s, expected: %s", policy2.Name, policy.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | func TestDisableRepPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	err := DisableRepPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to disable policy, id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	p, err := GetRepPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if p == nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to find a policy with id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if p.Enabled == 1 { | 
					
						
							|  |  |  | 		t.Errorf("The Enabled value of replication policy is still 1 after disabled, id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestEnableRepPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	err := EnableRepPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to disable policy, id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	p, err := GetRepPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if p == nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to find a policy with id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if p.Enabled == 0 { | 
					
						
							|  |  |  | 		t.Errorf("The Enabled value of replication policy is still 0 after disabled, id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAddRepPolicy2(t *testing.T) { | 
					
						
							|  |  |  | 	policy2 := models.RepPolicy{ | 
					
						
							|  |  |  | 		ProjectID:   3, | 
					
						
							|  |  |  | 		Enabled:     0, | 
					
						
							|  |  |  | 		TargetID:    3, | 
					
						
							|  |  |  | 		Description: "whatever", | 
					
						
							|  |  |  | 		Name:        "mypolicy", | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 	policyID2, err := AddRepPolicy(policy2) | 
					
						
							|  |  |  | 	t.Logf("added policy, id: %d", policyID2) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AddRepPolicy: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 	p, err := GetRepPolicy(policyID2) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID2) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if p == nil { | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		t.Errorf("Unable to find a policy with id: %d", policyID2) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	var tm time.Time | 
					
						
							|  |  |  | 	if p.StartTime.After(tm) { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected start_time: %v", p.StartTime) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAddRepJob(t *testing.T) { | 
					
						
							|  |  |  | 	job := models.RepJob{ | 
					
						
							|  |  |  | 		Repository: "library/ubuntu", | 
					
						
							|  |  |  | 		PolicyID:   policyID, | 
					
						
							|  |  |  | 		Operation:  "transfer", | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:45 +08:00
										 |  |  | 		TagList:    []string{"12.01", "14.04", "latest"}, | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	id, err := AddRepJob(job) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AddRepJob: %v", err) | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-27 18:46:07 +08:00
										 |  |  | 	jobID = id | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	j, err := GetRepJob(id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetRepJob: %v, id: %d", err, id) | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if j == nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to find a job with id: %d", id) | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:45 +08:00
										 |  |  | 	if j.Status != models.JobPending || j.Repository != "library/ubuntu" || j.PolicyID != policyID || j.Operation != "transfer" || len(j.TagList) != 3 { | 
					
						
							|  |  |  | 		t.Errorf("Expected data of job, id: %d, Status: %s, Repository: library/ubuntu, PolicyID: %d, Operation: transfer, taglist length 3"+ | 
					
						
							|  |  |  | 			"but in returned data:, Status: %s, Repository: %s, Operation: %s, PolicyID: %d, TagList: %v", id, models.JobPending, policyID, j.Status, j.Repository, j.Operation, j.PolicyID, j.TagList) | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestUpdateRepJobStatus(t *testing.T) { | 
					
						
							|  |  |  | 	err := UpdateRepJobStatus(jobID, models.JobFinished) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occured in UpdateRepJobStatus, error: %v, id: %d", err, jobID) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	j, err := GetRepJob(jobID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetRepJob: %v, id: %d", err, jobID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if j == nil { | 
					
						
							|  |  |  | 		t.Errorf("Unable to find a job with id: %d", jobID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if j.Status != models.JobFinished { | 
					
						
							|  |  |  | 		t.Errorf("Job's status: %s, expected: %s, id: %d", j.Status, models.JobFinished, jobID) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 	err = UpdateRepJobStatus(jobID, models.JobPending) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occured in UpdateRepJobStatus when update it back to status pending, error: %v, id: %d", err, jobID) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | func TestGetRepPolicyByProject(t *testing.T) { | 
					
						
							|  |  |  | 	p1, err := GetRepPolicyByProject(99) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occured in GetRepPolicyByProject:%v, project ID: %d", err, 99) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(p1) > 0 { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected length of policy list, expected: 0, in fact: %d, project id: %d", len(p1), 99) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p2, err := GetRepPolicyByProject(1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occuered in GetRepPolicyByProject:%v, project ID: %d", err, 2) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(p2) != 1 { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected length of policy list, expected: 1, in fact: %d, project id: %d", len(p2), 1) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if p2[0].ID != policyID { | 
					
						
							|  |  |  | 		t.Errorf("Unexpecred policy id in result, expected: %d, in fact: %d", policyID, p2[0].ID) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetRepJobByPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	jobs, err := GetRepJobByPolicy(999) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Error occured in GetRepJobByPolicy: %v, policy ID: %d", err, 999) | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(jobs) > 0 { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Unexpected length of jobs, expected: 0, in fact: %d", len(jobs)) | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	jobs, err = GetRepJobByPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Error occured in GetRepJobByPolicy: %v, policy ID: %d", err, policyID) | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(jobs) != 1 { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Unexpected length of jobs, expected: 1, in fact: %d", len(jobs)) | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if jobs[0].ID != jobID { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Unexpected job ID in the result, expected: %d, in fact: %d", jobID, jobs[0].ID) | 
					
						
							| 
									
										
										
										
											2016-05-16 19:34:24 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | func TestFilterRepJobs(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-07-31 19:58:33 +08:00
										 |  |  | 	jobs, _, err := FilterRepJobs(policyID, "", "", nil, nil, 1000, 0) | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Error occured in FilterRepJobs: %v, policy ID: %d", err, policyID) | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 	if len(jobs) != 1 { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected length of jobs, expected: 1, in fact: %d", len(jobs)) | 
					
						
							| 
									
										
										
										
											2016-06-17 18:58:16 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 	if jobs[0].ID != jobID { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected job ID in the result, expected: %d, in fact: %d", jobID, jobs[0].ID) | 
					
						
							| 
									
										
										
										
											2016-06-17 18:58:16 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | func TestDeleteRepJob(t *testing.T) { | 
					
						
							|  |  |  | 	err := DeleteRepJob(jobID) | 
					
						
							| 
									
										
										
										
											2016-06-17 11:34:52 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Errorf("Error occured in DeleteRepJob: %v, id: %d", err, jobID) | 
					
						
							| 
									
										
										
										
											2016-06-17 11:34:52 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 	t.Logf("deleted rep job, id: %d", jobID) | 
					
						
							|  |  |  | 	j, err := GetRepJob(jobID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occured in GetRepJob:%v", err) | 
					
						
							| 
									
										
										
										
											2016-06-17 11:34:52 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 	if j != nil { | 
					
						
							|  |  |  | 		t.Errorf("Able to find rep job after deletion, id: %d", jobID) | 
					
						
							| 
									
										
										
										
											2016-06-17 11:34:52 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | func TestGetRepoJobToStop(t *testing.T) { | 
					
						
							|  |  |  | 	jobs := [...]models.RepJob{ | 
					
						
							|  |  |  | 		models.RepJob{ | 
					
						
							|  |  |  | 			Repository: "library/ubuntu", | 
					
						
							|  |  |  | 			PolicyID:   policyID, | 
					
						
							|  |  |  | 			Operation:  "transfer", | 
					
						
							|  |  |  | 			Status:     models.JobRunning, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		models.RepJob{ | 
					
						
							|  |  |  | 			Repository: "library/ubuntu", | 
					
						
							|  |  |  | 			PolicyID:   policyID, | 
					
						
							|  |  |  | 			Operation:  "transfer", | 
					
						
							|  |  |  | 			Status:     models.JobFinished, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		models.RepJob{ | 
					
						
							|  |  |  | 			Repository: "library/ubuntu", | 
					
						
							|  |  |  | 			PolicyID:   policyID, | 
					
						
							|  |  |  | 			Operation:  "transfer", | 
					
						
							|  |  |  | 			Status:     models.JobCanceled, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	var i int64 | 
					
						
							| 
									
										
										
										
											2016-06-20 13:04:27 +08:00
										 |  |  | 	var ids []int64 | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 	for _, j := range jobs { | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 		i, err = AddRepJob(j) | 
					
						
							|  |  |  | 		ids = append(ids, i) | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			log.Errorf("Failed to add Job: %+v, error: %v", j, err) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	res, err := GetRepJobToStop(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Errorf("Failed to Get Jobs, error: %v", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	//time.Sleep(15 * time.Second)
 | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	if len(res) != 1 { | 
					
						
							|  |  |  | 		log.Errorf("Expected length of stoppable jobs, expected:1, in fact: %d", len(res)) | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	for _, id := range ids { | 
					
						
							|  |  |  | 		err = DeleteRepJob(id) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			log.Errorf("Failed to delete job, id: %d, error: %v", id, err) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-19 16:09:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | func TestDeleteRepTarget(t *testing.T) { | 
					
						
							|  |  |  | 	err := DeleteRepTarget(targetID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occured in DeleteRepTarget: %v, id: %d", err, targetID) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	t.Logf("deleted target, id: %d", targetID) | 
					
						
							|  |  |  | 	tgt, err := GetRepTarget(targetID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetTarget: %v, id: %d", err, targetID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if tgt != nil { | 
					
						
							|  |  |  | 		t.Errorf("Able to find target after deletion, id: %d", targetID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-13 17:32:22 +08:00
										 |  |  | func TestFilterRepPolicies(t *testing.T) { | 
					
						
							|  |  |  | 	_, err := FilterRepPolicies("name", 0) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-06 12:18:59 +08:00
										 |  |  | 		t.Fatalf("failed to filter policy: %v", err) | 
					
						
							| 
									
										
										
										
											2016-06-13 17:32:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestUpdateRepPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	policy := &models.RepPolicy{ | 
					
						
							|  |  |  | 		ID:   policyID, | 
					
						
							|  |  |  | 		Name: "new_policy_name", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := UpdateRepPolicy(policy); err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to update policy") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | func TestDeleteRepPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	err := DeleteRepPolicy(policyID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occured in DeleteRepPolicy: %v, id: %d", err, policyID) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	t.Logf("delete rep policy, id: %d", policyID) | 
					
						
							|  |  |  | 	p, err := GetRepPolicy(policyID) | 
					
						
							| 
									
										
										
										
											2016-06-13 16:49:46 +08:00
										 |  |  | 	if err != nil && err != orm.ErrNoRows { | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 		t.Errorf("Error occured in GetRepPolicy:%v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-18 11:43:45 +08:00
										 |  |  | 	if p != nil && p.Deleted != 1 { | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 		t.Errorf("Able to find rep policy after deletion, id: %d", policyID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | func TestResetRepJobs(t *testing.T) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	job1 := models.RepJob{ | 
					
						
							|  |  |  | 		Repository: "library/ubuntua", | 
					
						
							|  |  |  | 		PolicyID:   policyID, | 
					
						
							|  |  |  | 		Operation:  "transfer", | 
					
						
							|  |  |  | 		Status:     models.JobRunning, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	job2 := models.RepJob{ | 
					
						
							|  |  |  | 		Repository: "library/ubuntub", | 
					
						
							|  |  |  | 		PolicyID:   policyID, | 
					
						
							|  |  |  | 		Operation:  "transfer", | 
					
						
							|  |  |  | 		Status:     models.JobCanceled, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	id1, err := AddRepJob(job1) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 		t.Errorf("Failed to add job: %+v, error: %v", job1, err) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	id2, err := AddRepJob(job2) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 		t.Errorf("Failed to add job: %+v, error: %v", job2, err) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-17 18:54:29 +08:00
										 |  |  | 	err = ResetRunningJobs() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to reset running jobs, error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	j1, err := GetRepJob(id1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to get rep job, id: %d, error: %v", id1, err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if j1.Status != models.JobPending { | 
					
						
							|  |  |  | 		t.Errorf("The rep job: %d, status should be Pending, but infact: %s", id1, j1.Status) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	j2, err := GetRepJob(id2) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to get rep job, id: %d, error: %v", id2, err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if j2.Status == models.JobPending { | 
					
						
							|  |  |  | 		t.Errorf("The rep job: %d, status should be Canceled, but infact: %s", id2, j2.Status) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetJobByStatus(t *testing.T) { | 
					
						
							|  |  |  | 	r1, err := GetRepJobByStatus(models.JobPending, models.JobRunning) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to run GetRepJobByStatus, error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(r1) != 1 { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected length of result, expected 1, but in fact:%d", len(r1)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r2, err := GetRepJobByStatus(models.JobPending, models.JobCanceled) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Failed to run GetRepJobByStatus, error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(r2) != 2 { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected length of result, expected 2, but in fact:%d", len(r2)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, j := range r2 { | 
					
						
							|  |  |  | 		DeleteRepJob(j.ID) | 
					
						
							| 
									
										
										
										
											2016-05-10 19:38:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:20:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 17:35:16 +08:00
										 |  |  | func TestGetOrmer(t *testing.T) { | 
					
						
							|  |  |  | 	o := GetOrmer() | 
					
						
							|  |  |  | 	if o == nil { | 
					
						
							|  |  |  | 		t.Errorf("Error get ormer.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-23 15:56:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | func TestAddRepository(t *testing.T) { | 
					
						
							|  |  |  | 	repoRecord := models.RepoRecord{ | 
					
						
							|  |  |  | 		Name:        currentProject.Name + "/" + repositoryName, | 
					
						
							|  |  |  | 		OwnerName:   currentUser.Username, | 
					
						
							|  |  |  | 		ProjectName: currentProject.Name, | 
					
						
							|  |  |  | 		Description: "testing repo", | 
					
						
							|  |  |  | 		PullCount:   0, | 
					
						
							|  |  |  | 		StarCount:   0, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 17:38:06 +08:00
										 |  |  | 	err := AddRepository(repoRecord) | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in AddRepository: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newRepoRecord, err := GetRepositoryByName(currentProject.Name + "/" + repositoryName) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetRepositoryByName: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if newRepoRecord == nil { | 
					
						
							|  |  |  | 		t.Errorf("No repository found queried by repository name: %v", currentProject.Name+"/"+repositoryName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var currentRepository *models.RepoRecord | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetRepositoryByName(t *testing.T) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	currentRepository, err = GetRepositoryByName(currentProject.Name + "/" + repositoryName) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetRepositoryByName: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if currentRepository == nil { | 
					
						
							|  |  |  | 		t.Errorf("No repository found queried by repository name: %v", currentProject.Name+"/"+repositoryName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if currentRepository.Name != currentProject.Name+"/"+repositoryName { | 
					
						
							|  |  |  | 		t.Errorf("Repository name does not match, expected: %s, actual: %s", currentProject.Name+"/"+repositoryName, currentProject.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestIncreasePullCount(t *testing.T) { | 
					
						
							|  |  |  | 	if err := IncreasePullCount(currentRepository.Name); err != nil { | 
					
						
							|  |  |  | 		log.Errorf("Error happens when increasing pull count: %v", currentRepository.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	repository, err := GetRepositoryByName(currentRepository.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetRepositoryByName: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if repository.PullCount != 1 { | 
					
						
							|  |  |  | 		t.Errorf("repository pull count is not 1 after IncreasePullCount, expected: 1, actual: %d", repository.PullCount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRepositoryExists(t *testing.T) { | 
					
						
							|  |  |  | 	var exists bool | 
					
						
							| 
									
										
										
										
											2016-08-31 17:38:06 +08:00
										 |  |  | 	exists = RepositoryExists(currentRepository.Name) | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2016-10-10 15:40:18 +08:00
										 |  |  | 		t.Errorf("The repository with name: %s, does not exist", currentRepository.Name) | 
					
						
							| 
									
										
										
										
											2016-08-31 17:03:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDeleteRepository(t *testing.T) { | 
					
						
							|  |  |  | 	err := DeleteRepository(currentRepository.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in DeleteRepository: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	repository, err := GetRepositoryByName(currentRepository.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error occurred in GetRepositoryByName: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if repository != nil { | 
					
						
							|  |  |  | 		t.Errorf("repository is not nil after deletion, repository: %+v", repository) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |