loader_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package policy_test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "github.com/go-test/deep"
  7. "github.com/porter-dev/porter/api/server/authz/policy"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. "github.com/porter-dev/porter/internal/repository/test"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. type basicLoaderTest struct {
  14. description string
  15. roleKind types.RoleKind
  16. expErr bool
  17. expErrString string
  18. expErrStatusCode int
  19. expPolicy []*types.PolicyDocument
  20. }
  21. var basicLoaderTests = []basicLoaderTest{
  22. {
  23. description: "should load admin policy",
  24. roleKind: types.RoleAdmin,
  25. expPolicy: policy.AdminPolicy,
  26. },
  27. {
  28. description: "should load developer policy",
  29. roleKind: types.RoleDeveloper,
  30. expPolicy: policy.DeveloperPolicy,
  31. },
  32. {
  33. description: "should load viewer policy",
  34. roleKind: types.RoleViewer,
  35. expPolicy: policy.ViewerPolicy,
  36. },
  37. {
  38. description: "should not load custom policy for basic loader",
  39. roleKind: types.RoleCustom,
  40. expErr: true,
  41. expErrStatusCode: http.StatusForbidden,
  42. expErrString: "custom role not supported for user 1, project 1",
  43. },
  44. }
  45. func TestBasicPolicyDocumentLoader(t *testing.T) {
  46. assert := assert.New(t)
  47. for _, basicTest := range basicLoaderTests {
  48. // use the in-memory project repo
  49. projRepo := test.NewProjectRepository(true)
  50. loader := policy.NewBasicPolicyDocumentLoader(projRepo)
  51. project := &models.Project{
  52. Name: "test-project",
  53. }
  54. var err error
  55. project, err = projRepo.CreateProject(project)
  56. if err != nil {
  57. t.Fatalf("%v", err)
  58. }
  59. _, err = projRepo.CreateProjectRole(project, &models.Role{
  60. Role: types.Role{
  61. UserID: 1,
  62. ProjectID: 1,
  63. Kind: basicTest.roleKind,
  64. },
  65. })
  66. if err != nil {
  67. t.Fatalf("%v", err)
  68. }
  69. docs, reqErr := loader.LoadPolicyDocuments(1, 1)
  70. assert.Equal(
  71. reqErr != nil,
  72. basicTest.expErr,
  73. "[ %s ]: expected error was %t, got %t",
  74. basicTest.description,
  75. reqErr != nil,
  76. basicTest.expErr,
  77. )
  78. if reqErr != nil && basicTest.expErr {
  79. readableStr := reqErr.Error()
  80. expReadableStr := basicTest.expErrString
  81. assert.Equal(
  82. expReadableStr,
  83. readableStr,
  84. "[ %s ]: readable string not equal",
  85. basicTest.description,
  86. )
  87. // check that external and internal errors are returned as well
  88. assert.Equal(
  89. basicTest.expErrStatusCode,
  90. reqErr.GetStatusCode(),
  91. "[ %s ]: status code not equal",
  92. basicTest.description,
  93. )
  94. } else if !basicTest.expErr {
  95. if diff := deep.Equal(basicTest.expPolicy, docs); diff != nil {
  96. t.Errorf("[ %s ]: policy documents not equal:", basicTest.description)
  97. t.Error(diff)
  98. }
  99. }
  100. }
  101. }
  102. func TestErrorForbiddenInvalidRole(t *testing.T) {
  103. assert := assert.New(t)
  104. // use the in-memory project repo
  105. projRepo := test.NewProjectRepository(true)
  106. loader := policy.NewBasicPolicyDocumentLoader(projRepo)
  107. project := &models.Project{
  108. Name: "test-project",
  109. }
  110. var err error
  111. project, err = projRepo.CreateProject(project)
  112. if err != nil {
  113. t.Fatalf("%v", err)
  114. }
  115. _, err = projRepo.CreateProjectRole(project, &models.Role{
  116. Role: types.Role{
  117. UserID: 1,
  118. ProjectID: 1,
  119. Kind: types.RoleAdmin,
  120. },
  121. })
  122. if err != nil {
  123. t.Fatalf("%v", err)
  124. }
  125. _, reqErr := loader.LoadPolicyDocuments(2, 1)
  126. if reqErr == nil {
  127. t.Fatalf("Expected forbidden error for invalid project role")
  128. }
  129. // check that external and internal errors are returned as well
  130. assert.Equal(
  131. http.StatusForbidden,
  132. reqErr.GetStatusCode(),
  133. "status is not status forbidden",
  134. )
  135. assert.Equal(
  136. fmt.Sprintf("user %d does not have a role in project %d", 2, 1),
  137. reqErr.Error(),
  138. "error message is not correct",
  139. )
  140. }
  141. func TestErrorCannotQuery(t *testing.T) {
  142. assert := assert.New(t)
  143. // use the in-memory project repo
  144. projRepo := test.NewProjectRepository(false)
  145. loader := policy.NewBasicPolicyDocumentLoader(projRepo)
  146. _, reqErr := loader.LoadPolicyDocuments(2, 1)
  147. if reqErr == nil {
  148. t.Fatalf("Expected internal error for failing to query")
  149. }
  150. // check that external and internal errors are returned as well
  151. assert.Equal(
  152. http.StatusInternalServerError,
  153. reqErr.GetStatusCode(),
  154. "status is not status internal",
  155. )
  156. }