PageRenderTime 243ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/dao/dao_test.go

https://gitlab.com/e0/harbor
Go | 724 lines | 623 code | 85 blank | 16 comment | 227 complexity | 5e561f9c945ddc9370c54665e94cb831 MD5 | raw file
  1. /*
  2. Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dao
  14. import (
  15. "os"
  16. "testing"
  17. "time"
  18. "github.com/vmware/harbor/utils/log"
  19. "github.com/vmware/harbor/models"
  20. "github.com/astaxie/beego/orm"
  21. )
  22. func execUpdate(o orm.Ormer, sql string, params interface{}) error {
  23. p, err := o.Raw(sql).Prepare()
  24. if err != nil {
  25. return err
  26. }
  27. defer p.Close()
  28. _, err = p.Exec(params)
  29. if err != nil {
  30. return err
  31. }
  32. return nil
  33. }
  34. func clearUp(username string) {
  35. var err error
  36. o := orm.NewOrm()
  37. o.Begin()
  38. err = execUpdate(o, `delete pm
  39. from project_member pm
  40. join user u
  41. on pm.user_id = u.user_id
  42. where u.username = ?`, username)
  43. if err != nil {
  44. o.Rollback()
  45. log.Error(err)
  46. }
  47. err = execUpdate(o, `delete pm
  48. from project_member pm
  49. join project p
  50. on pm.project_id = p.project_id
  51. where p.name = ?`, projectName)
  52. if err != nil {
  53. o.Rollback()
  54. log.Error(err)
  55. }
  56. err = execUpdate(o, `delete al
  57. from access_log al
  58. join user u
  59. on al.user_id = u.user_id
  60. where u.username = ?`, username)
  61. if err != nil {
  62. o.Rollback()
  63. log.Error(err)
  64. }
  65. err = execUpdate(o, `delete al
  66. from access_log al
  67. join project p
  68. on al.project_id = p.project_id
  69. where p.name = ?`, projectName)
  70. if err != nil {
  71. o.Rollback()
  72. log.Error(err)
  73. }
  74. err = execUpdate(o, `delete from project where name = ?`, projectName)
  75. if err != nil {
  76. o.Rollback()
  77. log.Error(err)
  78. }
  79. err = execUpdate(o, `delete from user where username = ?`, username)
  80. if err != nil {
  81. o.Rollback()
  82. log.Error(err)
  83. }
  84. o.Commit()
  85. }
  86. const username string = "Tester01"
  87. const projectName string = "test_project"
  88. const repoTag string = "test1.1"
  89. const repoTag2 string = "test1.2"
  90. const SysAdmin int = 1
  91. const projectAdmin int = 2
  92. const developer int = 3
  93. const guest int = 4
  94. const publicityOn = 1
  95. const publicityOff = 0
  96. func TestMain(m *testing.M) {
  97. dbHost := os.Getenv("DB_HOST")
  98. if len(dbHost) == 0 {
  99. log.Fatalf("environment variable DB_HOST is not set")
  100. }
  101. dbUser := os.Getenv("DB_USR")
  102. if len(dbUser) == 0 {
  103. log.Fatalf("environment variable DB_USR is not set")
  104. }
  105. dbPort := os.Getenv("DB_PORT")
  106. if len(dbPort) == 0 {
  107. log.Fatalf("environment variable DB_PORT is not set")
  108. }
  109. dbPassword := os.Getenv("DB_PWD")
  110. log.Infof("DB_HOST: %s, DB_USR: %s, DB_PORT: %s, DB_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
  111. os.Setenv("MYSQL_HOST", dbHost)
  112. os.Setenv("MYSQL_PORT", dbPort)
  113. os.Setenv("MYSQL_USR", dbUser)
  114. os.Setenv("MYSQL_PWD", dbPassword)
  115. os.Setenv("AUTH_MODE", "db_auth")
  116. InitDB()
  117. clearUp(username)
  118. os.Exit(m.Run())
  119. }
  120. func TestRegister(t *testing.T) {
  121. user := models.User{
  122. Username: username,
  123. Email: "tester01@vmware.com",
  124. Password: "Abc12345",
  125. Realname: "tester01",
  126. Comment: "register",
  127. }
  128. _, err := Register(user)
  129. if err != nil {
  130. t.Errorf("Error occurred in Register: %v", err)
  131. }
  132. //Check if user registered successfully.
  133. queryUser := models.User{
  134. Username: username,
  135. }
  136. newUser, err := GetUser(queryUser)
  137. if err != nil {
  138. t.Errorf("Error occurred in GetUser: %v", err)
  139. }
  140. if newUser.Username != username {
  141. t.Errorf("Username does not match, expected: %s, actual: %s", username, newUser.Username)
  142. }
  143. if newUser.Email != "tester01@vmware.com" {
  144. t.Errorf("Email does not match, expected: %s, actual: %s", "tester01@vmware.com", newUser.Email)
  145. }
  146. }
  147. func TestUserExists(t *testing.T) {
  148. var exists bool
  149. var err error
  150. exists, err = UserExists(models.User{Username: username}, "username")
  151. if err != nil {
  152. t.Errorf("Error occurred in UserExists: %v", err)
  153. }
  154. if !exists {
  155. t.Errorf("User %s was inserted but does not exist", username)
  156. }
  157. exists, err = UserExists(models.User{Email: "tester01@vmware.com"}, "email")
  158. if err != nil {
  159. t.Errorf("Error occurred in UserExists: %v", err)
  160. }
  161. if !exists {
  162. t.Errorf("User with email %s inserted but does not exist", "tester01@vmware.com")
  163. }
  164. exists, err = UserExists(models.User{Username: "NOTHERE"}, "username")
  165. if err != nil {
  166. t.Errorf("Error occurred in UserExists: %v", err)
  167. }
  168. if exists {
  169. t.Errorf("User %s was not inserted but does exist", "NOTHERE")
  170. }
  171. }
  172. func TestLoginByUserName(t *testing.T) {
  173. userQuery := models.User{
  174. Username: username,
  175. Password: "Abc12345",
  176. }
  177. loginUser, err := LoginByDb(models.AuthModel{
  178. Principal: userQuery.Username,
  179. Password: userQuery.Password,
  180. })
  181. if err != nil {
  182. t.Errorf("Error occurred in LoginByDb: %v", err)
  183. }
  184. if loginUser == nil {
  185. t.Errorf("No found for user logined by username and password: %v", userQuery)
  186. }
  187. if loginUser.Username != username {
  188. t.Errorf("User's username does not match after login, expected: %s, actual: %s", username, loginUser.Username)
  189. }
  190. }
  191. func TestLoginByEmail(t *testing.T) {
  192. userQuery := models.User{
  193. Email: "tester01@vmware.com",
  194. Password: "Abc12345",
  195. }
  196. loginUser, err := LoginByDb(models.AuthModel{
  197. Principal: userQuery.Email,
  198. Password: userQuery.Password,
  199. })
  200. if err != nil {
  201. t.Errorf("Error occurred in LoginByDb: %v", err)
  202. }
  203. if loginUser == nil {
  204. t.Errorf("No found for user logined by email and password : %v", userQuery)
  205. }
  206. if loginUser.Username != username {
  207. t.Errorf("User's username does not match after login, expected: %s, actual: %s", username, loginUser.Username)
  208. }
  209. }
  210. var currentUser *models.User
  211. func TestGetUser(t *testing.T) {
  212. queryUser := models.User{
  213. Username: username,
  214. }
  215. var err error
  216. currentUser, err = GetUser(queryUser)
  217. if err != nil {
  218. t.Errorf("Error occurred in GetUser: %v", err)
  219. }
  220. if currentUser == nil {
  221. t.Errorf("No user found queried by user query: %+v", queryUser)
  222. }
  223. if currentUser.Email != "tester01@vmware.com" {
  224. t.Errorf("the user's email does not match, expected: tester01@vmware.com, actual: %s", currentUser.Email)
  225. }
  226. }
  227. func TestListUsers(t *testing.T) {
  228. users, err := ListUsers(models.User{})
  229. if err != nil {
  230. t.Errorf("Error occurred in ListUsers: %v", err)
  231. }
  232. if len(users) != 1 {
  233. t.Errorf("Expect one user in list, but the acutal length is %d, the list: %+v", len(users), users)
  234. }
  235. users2, err := ListUsers(models.User{Username: username})
  236. if len(users2) != 1 {
  237. t.Errorf("Expect one user in list, but the acutal length is %d, the list: %+v", len(users), users)
  238. }
  239. if users2[0].Username != username {
  240. t.Errorf("The username in result list does not match, expected: %s, actual: %s", username, users2[0].Username)
  241. }
  242. }
  243. func TestResetUserPassword(t *testing.T) {
  244. uuid, err := GenerateRandomString()
  245. if err != nil {
  246. t.Errorf("Error occurred in GenerateRandomString: %v", err)
  247. }
  248. err = UpdateUserResetUUID(models.User{ResetUUID: uuid, Email: currentUser.Email})
  249. if err != nil {
  250. t.Errorf("Error occurred in UpdateUserResetUuid: %v", err)
  251. }
  252. err = ResetUserPassword(models.User{UserID: currentUser.UserID, Password: "HarborTester12345", ResetUUID: uuid, Salt: currentUser.Salt})
  253. if err != nil {
  254. t.Errorf("Error occurred in ResetUserPassword: %v", err)
  255. }
  256. loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "HarborTester12345"})
  257. if err != nil {
  258. t.Errorf("Error occurred in LoginByDb: %v", err)
  259. }
  260. if loginedUser.Username != username {
  261. t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
  262. }
  263. }
  264. func TestChangeUserPassword(t *testing.T) {
  265. err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewHarborTester12345", Salt: currentUser.Salt})
  266. if err != nil {
  267. t.Errorf("Error occurred in ChangeUserPassword: %v", err)
  268. }
  269. loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewHarborTester12345"})
  270. if err != nil {
  271. t.Errorf("Error occurred in LoginByDb: %v", err)
  272. }
  273. if loginedUser.Username != username {
  274. t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
  275. }
  276. }
  277. func TestChangeUserPasswordWithOldPassword(t *testing.T) {
  278. err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NewerHarborTester12345", Salt: currentUser.Salt}, "NewHarborTester12345")
  279. if err != nil {
  280. t.Errorf("Error occurred in ChangeUserPassword: %v", err)
  281. }
  282. loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewerHarborTester12345"})
  283. if err != nil {
  284. t.Errorf("Error occurred in LoginByDb: %v", err)
  285. }
  286. if loginedUser.Username != username {
  287. t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", username, loginedUser.Username)
  288. }
  289. }
  290. func TestChangeUserPasswordWithIncorrectOldPassword(t *testing.T) {
  291. err := ChangeUserPassword(models.User{UserID: currentUser.UserID, Password: "NNewerHarborTester12345", Salt: currentUser.Salt}, "WrongNewerHarborTester12345")
  292. if err == nil {
  293. t.Errorf("Error does not occurred due to old password is incorrect.")
  294. }
  295. loginedUser, err := LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NNewerHarborTester12345"})
  296. if err != nil {
  297. t.Errorf("Error occurred in LoginByDb: %v", err)
  298. }
  299. if loginedUser != nil {
  300. t.Errorf("The login user is not nil, acutal: %+v", loginedUser)
  301. }
  302. }
  303. func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) {
  304. projects, err := QueryRelevantProjects(currentUser.UserID)
  305. if err != nil {
  306. t.Errorf("Error occurred in QueryRelevantProjects: %v", err)
  307. }
  308. if len(projects) != 1 {
  309. t.Errorf("Expected only one project in DB, but actual: %d", len(projects))
  310. }
  311. if projects[0].Name != "library" {
  312. t.Errorf("There name of the project does not match, expected: %s, actual: %s", "library", projects[0].Name)
  313. }
  314. }
  315. func TestAddProject(t *testing.T) {
  316. project := models.Project{
  317. OwnerID: currentUser.UserID,
  318. Name: projectName,
  319. CreationTime: time.Now(),
  320. OwnerName: currentUser.Username,
  321. }
  322. _, err := AddProject(project)
  323. if err != nil {
  324. t.Errorf("Error occurred in AddProject: %v", err)
  325. }
  326. newProject, err := GetProjectByName(projectName)
  327. if err != nil {
  328. t.Errorf("Error occurred in GetProjectByName: %v", err)
  329. }
  330. if newProject == nil {
  331. t.Errorf("No project found queried by project name: %v", projectName)
  332. }
  333. }
  334. var currentProject *models.Project
  335. func TestGetProject(t *testing.T) {
  336. var err error
  337. currentProject, err = GetProjectByName(projectName)
  338. if err != nil {
  339. t.Errorf("Error occurred in GetProjectByName: %v", err)
  340. }
  341. if currentProject == nil {
  342. t.Errorf("No project found queried by project name: %v", projectName)
  343. }
  344. if currentProject.Name != projectName {
  345. t.Errorf("Project name does not match, expected: %s, actual: %s", projectName, currentProject.Name)
  346. }
  347. }
  348. func TestGetAccessLog(t *testing.T) {
  349. queryAccessLog := models.AccessLog{
  350. UserID: currentUser.UserID,
  351. ProjectID: currentProject.ProjectID,
  352. }
  353. accessLogs, err := GetAccessLogs(queryAccessLog)
  354. if err != nil {
  355. t.Errorf("Error occurred in GetAccessLog: %v", err)
  356. }
  357. if len(accessLogs) != 1 {
  358. t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogs))
  359. }
  360. if accessLogs[0].RepoName != projectName+"/" {
  361. t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogs[0].RepoName)
  362. }
  363. }
  364. func TestAddAccessLog(t *testing.T) {
  365. var err error
  366. var accessLogList []models.AccessLog
  367. accessLog := models.AccessLog{
  368. UserID: currentUser.UserID,
  369. ProjectID: currentProject.ProjectID,
  370. RepoName: currentProject.Name + "/",
  371. RepoTag: repoTag,
  372. GUID: "N/A",
  373. Operation: "create",
  374. OpTime: time.Now(),
  375. }
  376. err = AddAccessLog(accessLog)
  377. if err != nil {
  378. t.Errorf("Error occurred in AddAccessLog: %v", err)
  379. }
  380. accessLogList, err = GetAccessLogs(accessLog)
  381. if err != nil {
  382. t.Errorf("Error occurred in GetAccessLog: %v", err)
  383. }
  384. if len(accessLogList) != 1 {
  385. t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogList))
  386. }
  387. if accessLogList[0].RepoName != projectName+"/" {
  388. t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogList[0].RepoName)
  389. }
  390. if accessLogList[0].RepoTag != repoTag {
  391. t.Errorf("The repo tag does not match, expected: %s, actual: %s", repoTag, accessLogList[0].RepoTag)
  392. }
  393. }
  394. func TestAccessLog(t *testing.T) {
  395. var err error
  396. var accessLogList []models.AccessLog
  397. accessLog := models.AccessLog{
  398. UserID: currentUser.UserID,
  399. ProjectID: currentProject.ProjectID,
  400. RepoName: currentProject.Name + "/",
  401. RepoTag: repoTag2,
  402. Operation: "create",
  403. }
  404. err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/", repoTag2, "create")
  405. if err != nil {
  406. t.Errorf("Error occurred in AccessLog: %v", err)
  407. }
  408. accessLogList, err = GetAccessLogs(accessLog)
  409. if err != nil {
  410. t.Errorf("Error occurred in GetAccessLog: %v", err)
  411. }
  412. if len(accessLogList) != 1 {
  413. t.Errorf("The length of accesslog list should be 1, actual: %d", len(accessLogList))
  414. }
  415. if accessLogList[0].RepoName != projectName+"/" {
  416. t.Errorf("The project name does not match, expected: %s, actual: %s", projectName+"/", accessLogList[0].RepoName)
  417. }
  418. if accessLogList[0].RepoTag != repoTag2 {
  419. t.Errorf("The repo tag does not match, expected: %s, actual: %s", repoTag2, accessLogList[0].RepoTag)
  420. }
  421. }
  422. func TestProjectExists(t *testing.T) {
  423. var exists bool
  424. var err error
  425. exists, err = ProjectExists(currentProject.ProjectID)
  426. if err != nil {
  427. t.Errorf("Error occurred in ProjectExists: %v", err)
  428. }
  429. if !exists {
  430. t.Errorf("The project with id: %d, does not exist", currentProject.ProjectID)
  431. }
  432. exists, err = ProjectExists(currentProject.Name)
  433. if err != nil {
  434. t.Errorf("Error occurred in ProjectExists: %v", err)
  435. }
  436. if !exists {
  437. t.Errorf("The project with name: %s, does not exist", currentProject.Name)
  438. }
  439. }
  440. func TestGetProjectById(t *testing.T) {
  441. id := currentProject.ProjectID
  442. p, err := GetProjectByID(id)
  443. if err != nil {
  444. t.Errorf("Error in GetProjectById: %v, id: %d", err, id)
  445. }
  446. if p.Name != currentProject.Name {
  447. t.Errorf("project name does not match, expected: %s, actual: %s", currentProject.Name, p.Name)
  448. }
  449. }
  450. func TestGetUserByProject(t *testing.T) {
  451. pid := currentProject.ProjectID
  452. u1 := models.User{
  453. Username: "%%Tester%%",
  454. }
  455. u2 := models.User{
  456. Username: "nononono",
  457. }
  458. users, err := GetUserByProject(pid, u1)
  459. if err != nil {
  460. t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u1)
  461. }
  462. if len(users) != 1 {
  463. t.Errorf("unexpected length of user list, expected: 1, the users list: %+v", users)
  464. }
  465. users, err = GetUserByProject(pid, u2)
  466. if err != nil {
  467. t.Errorf("Error happened in GetUserByProject: %v, project Id: %d, user: %+v", err, pid, u2)
  468. }
  469. if len(users) != 0 {
  470. t.Errorf("unexpected length of user list, expected: 0, the users list: %+v", users)
  471. }
  472. }
  473. func TestToggleProjectPublicity(t *testing.T) {
  474. err := ToggleProjectPublicity(currentProject.ProjectID, publicityOn)
  475. if err != nil {
  476. t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
  477. }
  478. currentProject, err = GetProjectByName(projectName)
  479. if err != nil {
  480. t.Errorf("Error occurred in GetProjectByName: %v", err)
  481. }
  482. if currentProject.Public != publicityOn {
  483. t.Errorf("project, id: %d, its publicity is not on", currentProject.ProjectID)
  484. }
  485. err = ToggleProjectPublicity(currentProject.ProjectID, publicityOff)
  486. if err != nil {
  487. t.Errorf("Error occurred in ToggleProjectPublicity: %v", err)
  488. }
  489. currentProject, err = GetProjectByName(projectName)
  490. if err != nil {
  491. t.Errorf("Error occurred in GetProjectByName: %v", err)
  492. }
  493. if currentProject.Public != publicityOff {
  494. t.Errorf("project, id: %d, its publicity is not off", currentProject.ProjectID)
  495. }
  496. }
  497. func TestIsProjectPublic(t *testing.T) {
  498. if isPublic := IsProjectPublic(projectName); isPublic {
  499. t.Errorf("project, id: %d, its publicity is not false after turning off", currentProject.ProjectID)
  500. }
  501. }
  502. func TestQueryProject(t *testing.T) {
  503. query1 := models.Project{
  504. UserID: 1,
  505. }
  506. projects, err := QueryProject(query1)
  507. if err != nil {
  508. t.Errorf("Error in Query Project: %v, query: %+v", err, query1)
  509. }
  510. if len(projects) != 2 {
  511. t.Errorf("Expecting get 2 projects, but actual: %d, the list: %+v", len(projects), projects)
  512. }
  513. query2 := models.Project{
  514. Public: 1,
  515. }
  516. projects, err = QueryProject(query2)
  517. if err != nil {
  518. t.Errorf("Error in Query Project: %v, query: %+v", err, query2)
  519. }
  520. if len(projects) != 1 {
  521. t.Errorf("Expecting get 1 project, but actual: %d, the list: %+v", len(projects), projects)
  522. }
  523. query3 := models.Project{
  524. UserID: 9,
  525. }
  526. projects, err = QueryProject(query3)
  527. if err != nil {
  528. t.Errorf("Error in Query Project: %v, query: %+v", err, query3)
  529. }
  530. if len(projects) != 0 {
  531. t.Errorf("Expecting get 0 project, but actual: %d, the list: %+v", len(projects), projects)
  532. }
  533. }
  534. func TestGetUserProjectRoles(t *testing.T) {
  535. r, err := GetUserProjectRoles(currentUser.UserID, currentProject.ProjectID)
  536. if err != nil {
  537. t.Errorf("Error happened in GetUserProjectRole: %v, userID: %+v, project Id: %d", err, currentUser.UserID, currentProject.ProjectID)
  538. }
  539. //Get the size of current user project role.
  540. if len(r) != 1 {
  541. t.Errorf("The user, id: %d, should only have one role in project, id: %d, but actual: %d", currentUser.UserID, currentProject.ProjectID, len(r))
  542. }
  543. if r[0].Name != "projectAdmin" {
  544. t.Errorf("the expected rolename is: projectAdmin, actual: %s", r[0].Name)
  545. }
  546. }
  547. func TestProjectPermission(t *testing.T) {
  548. roleCode, err := GetPermission(currentUser.Username, currentProject.Name)
  549. if err != nil {
  550. t.Errorf("Error occurred in GetPermission: %v", err)
  551. }
  552. if roleCode != "MDRWS" {
  553. t.Errorf("The expected role code is MDRWS,but actual: %s", roleCode)
  554. }
  555. }
  556. func TestQueryRelevantProjects(t *testing.T) {
  557. projects, err := QueryRelevantProjects(currentUser.UserID)
  558. if err != nil {
  559. t.Errorf("Error occurred in QueryRelevantProjects: %v", err)
  560. }
  561. if len(projects) != 2 {
  562. t.Errorf("Expected length of relevant projects is 2, but actual: %d, the projects: %+v", len(projects), projects)
  563. }
  564. if projects[1].Name != projectName {
  565. t.Errorf("Expected project name in the list: %s, actual: %s", projectName, projects[1].Name)
  566. }
  567. }
  568. func TestAddProjectMember(t *testing.T) {
  569. err := AddProjectMember(currentProject.ProjectID, 1, models.DEVELOPER)
  570. if err != nil {
  571. t.Errorf("Error occurred in AddProjectMember: %v", err)
  572. }
  573. roles, err := GetUserProjectRoles(1, currentProject.ProjectID)
  574. if err != nil {
  575. t.Errorf("Error occurred in GetUserProjectRoles: %v", err)
  576. }
  577. flag := false
  578. for _, role := range roles {
  579. if role.Name == "developer" {
  580. flag = true
  581. break
  582. }
  583. }
  584. if !flag {
  585. t.Errorf("the user which ID is 1 does not have developer privileges")
  586. }
  587. }
  588. func TestDeleteProjectMember(t *testing.T) {
  589. err := DeleteProjectMember(currentProject.ProjectID, 1)
  590. if err != nil {
  591. t.Errorf("Error occurred in DeleteProjectMember: %v", err)
  592. }
  593. roles, err := GetUserProjectRoles(1, currentProject.ProjectID)
  594. if err != nil {
  595. t.Errorf("Error occurred in GetUserProjectRoles: %v", err)
  596. }
  597. if len(roles) != 0 {
  598. t.Errorf("delete record failed from table project_member")
  599. }
  600. }
  601. func TestToggleAdminRole(t *testing.T) {
  602. err := ToggleUserAdminRole(*currentUser)
  603. if err != nil {
  604. t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser)
  605. }
  606. isAdmin, err := IsAdminRole(currentUser.UserID)
  607. if err != nil {
  608. t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID)
  609. }
  610. if !isAdmin {
  611. t.Errorf("User is not admin after toggled, user id: %d", currentUser.UserID)
  612. }
  613. err = ToggleUserAdminRole(*currentUser)
  614. if err != nil {
  615. t.Errorf("Error in toggle ToggleUserAdmin role: %v, user: %+v", err, currentUser)
  616. }
  617. isAdmin, err = IsAdminRole(currentUser.UserID)
  618. if err != nil {
  619. t.Errorf("Error in IsAdminRole: %v, user id: %d", err, currentUser.UserID)
  620. }
  621. if isAdmin {
  622. t.Errorf("User is still admin after toggled, user id: %d", currentUser.UserID)
  623. }
  624. }
  625. func TestDeleteUser(t *testing.T) {
  626. err := DeleteUser(currentUser.UserID)
  627. if err != nil {
  628. t.Errorf("Error occurred in DeleteUser: %v", err)
  629. }
  630. user, err := GetUser(*currentUser)
  631. if err != nil {
  632. t.Errorf("Error occurred in GetUser: %v", err)
  633. }
  634. if user != nil {
  635. t.Errorf("user is not nil after deletion, user: %+v", user)
  636. }
  637. }