本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAccessLevel(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  12. user5 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  13. // A public repository owned by User 2
  14. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  15. assert.False(t, repo1.IsPrivate)
  16. // A private repository owned by Org 3
  17. repo3 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  18. assert.True(t, repo3.IsPrivate)
  19. level, err := AccessLevel(user2, repo1)
  20. assert.NoError(t, err)
  21. assert.Equal(t, AccessModeOwner, level)
  22. level, err = AccessLevel(user2, repo3)
  23. assert.NoError(t, err)
  24. assert.Equal(t, AccessModeOwner, level)
  25. level, err = AccessLevel(user5, repo1)
  26. assert.NoError(t, err)
  27. assert.Equal(t, AccessModeRead, level)
  28. level, err = AccessLevel(user5, repo3)
  29. assert.NoError(t, err)
  30. assert.Equal(t, AccessModeNone, level)
  31. }
  32. func TestHasAccess(t *testing.T) {
  33. assert.NoError(t, PrepareTestDatabase())
  34. user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  35. user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  36. // A public repository owned by User 2
  37. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  38. assert.False(t, repo1.IsPrivate)
  39. // A private repository owned by Org 3
  40. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  41. assert.True(t, repo2.IsPrivate)
  42. has, err := HasAccess(user1.ID, repo1)
  43. assert.NoError(t, err)
  44. assert.True(t, has)
  45. _, err = HasAccess(user1.ID, repo2)
  46. assert.NoError(t, err)
  47. _, err = HasAccess(user2.ID, repo1)
  48. assert.NoError(t, err)
  49. _, err = HasAccess(user2.ID, repo2)
  50. assert.NoError(t, err)
  51. }
  52. func TestUser_GetRepositoryAccesses(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  55. accesses, err := user1.GetRepositoryAccesses()
  56. assert.NoError(t, err)
  57. assert.Len(t, accesses, 0)
  58. }
  59. func TestUser_GetAccessibleRepositories(t *testing.T) {
  60. assert.NoError(t, PrepareTestDatabase())
  61. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  62. repos, err := user1.GetAccessibleRepositories(0)
  63. assert.NoError(t, err)
  64. assert.Len(t, repos, 0)
  65. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  66. repos, err = user2.GetAccessibleRepositories(0)
  67. assert.NoError(t, err)
  68. assert.Len(t, repos, 1)
  69. }
  70. func TestRepository_RecalculateAccesses(t *testing.T) {
  71. // test with organization repo
  72. assert.NoError(t, PrepareTestDatabase())
  73. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  74. assert.NoError(t, repo1.GetOwner())
  75. _, err := x.Delete(&Collaboration{UserID: 2, RepoID: 3})
  76. assert.NoError(t, err)
  77. assert.NoError(t, repo1.RecalculateAccesses())
  78. access := &Access{UserID: 2, RepoID: 3}
  79. has, err := x.Get(access)
  80. assert.NoError(t, err)
  81. assert.True(t, has)
  82. assert.Equal(t, AccessModeOwner, access.Mode)
  83. }
  84. func TestRepository_RecalculateAccesses2(t *testing.T) {
  85. // test with non-organization repo
  86. assert.NoError(t, PrepareTestDatabase())
  87. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  88. assert.NoError(t, repo1.GetOwner())
  89. _, err := x.Delete(&Collaboration{UserID: 4, RepoID: 4})
  90. assert.NoError(t, err)
  91. assert.NoError(t, repo1.RecalculateAccesses())
  92. has, err := x.Get(&Access{UserID: 4, RepoID: 4})
  93. assert.NoError(t, err)
  94. assert.False(t, has)
  95. }
上海开阖软件有限公司 沪ICP备12045867号-1