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.

152 lines
4.5 KiB

  1. // Copyright 2018 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 integrations
  5. import (
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAPIOrgCreate(t *testing.T) {
  16. onGiteaRun(t, func(*testing.T, *url.URL) {
  17. session := loginUser(t, "user1")
  18. token := getTokenForLoggedInUser(t, session)
  19. var org = api.CreateOrgOption{
  20. UserName: "user1_org",
  21. FullName: "User1's organization",
  22. Description: "This organization created by user1",
  23. Website: "https://try.gitea.io",
  24. Location: "Shanghai",
  25. Visibility: "limited",
  26. }
  27. req := NewRequestWithJSON(t, "POST", "/api/v1/orgs?token="+token, &org)
  28. resp := session.MakeRequest(t, req, http.StatusCreated)
  29. var apiOrg api.Organization
  30. DecodeJSON(t, resp, &apiOrg)
  31. assert.Equal(t, org.UserName, apiOrg.UserName)
  32. assert.Equal(t, org.FullName, apiOrg.FullName)
  33. assert.Equal(t, org.Description, apiOrg.Description)
  34. assert.Equal(t, org.Website, apiOrg.Website)
  35. assert.Equal(t, org.Location, apiOrg.Location)
  36. assert.Equal(t, org.Visibility, apiOrg.Visibility)
  37. models.AssertExistsAndLoadBean(t, &models.User{
  38. Name: org.UserName,
  39. LowerName: strings.ToLower(org.UserName),
  40. FullName: org.FullName,
  41. })
  42. req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName)
  43. resp = session.MakeRequest(t, req, http.StatusOK)
  44. DecodeJSON(t, resp, &apiOrg)
  45. assert.EqualValues(t, org.UserName, apiOrg.UserName)
  46. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName)
  47. resp = session.MakeRequest(t, req, http.StatusOK)
  48. var repos []*api.Repository
  49. DecodeJSON(t, resp, &repos)
  50. for _, repo := range repos {
  51. assert.False(t, repo.Private)
  52. }
  53. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName)
  54. resp = session.MakeRequest(t, req, http.StatusOK)
  55. // user1 on this org is public
  56. var users []*api.User
  57. DecodeJSON(t, resp, &users)
  58. assert.EqualValues(t, 1, len(users))
  59. assert.EqualValues(t, "user1", users[0].UserName)
  60. })
  61. }
  62. func TestAPIOrgEdit(t *testing.T) {
  63. onGiteaRun(t, func(*testing.T, *url.URL) {
  64. session := loginUser(t, "user1")
  65. token := getTokenForLoggedInUser(t, session)
  66. var org = api.EditOrgOption{
  67. FullName: "User3 organization new full name",
  68. Description: "A new description",
  69. Website: "https://try.gitea.io/new",
  70. Location: "Beijing",
  71. Visibility: "private",
  72. }
  73. req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/user3?token="+token, &org)
  74. resp := session.MakeRequest(t, req, http.StatusOK)
  75. var apiOrg api.Organization
  76. DecodeJSON(t, resp, &apiOrg)
  77. assert.Equal(t, "user3", apiOrg.UserName)
  78. assert.Equal(t, org.FullName, apiOrg.FullName)
  79. assert.Equal(t, org.Description, apiOrg.Description)
  80. assert.Equal(t, org.Website, apiOrg.Website)
  81. assert.Equal(t, org.Location, apiOrg.Location)
  82. assert.Equal(t, org.Visibility, apiOrg.Visibility)
  83. })
  84. }
  85. func TestAPIOrgEditBadVisibility(t *testing.T) {
  86. onGiteaRun(t, func(*testing.T, *url.URL) {
  87. session := loginUser(t, "user1")
  88. token := getTokenForLoggedInUser(t, session)
  89. var org = api.EditOrgOption{
  90. FullName: "User3 organization new full name",
  91. Description: "A new description",
  92. Website: "https://try.gitea.io/new",
  93. Location: "Beijing",
  94. Visibility: "badvisibility",
  95. }
  96. req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/user3?token="+token, &org)
  97. session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  98. })
  99. }
  100. func TestAPIOrgDeny(t *testing.T) {
  101. onGiteaRun(t, func(*testing.T, *url.URL) {
  102. setting.Service.RequireSignInView = true
  103. defer func() {
  104. setting.Service.RequireSignInView = false
  105. }()
  106. var orgName = "user1_org"
  107. req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName)
  108. MakeRequest(t, req, http.StatusNotFound)
  109. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName)
  110. MakeRequest(t, req, http.StatusNotFound)
  111. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName)
  112. MakeRequest(t, req, http.StatusNotFound)
  113. })
  114. }
  115. func TestAPIGetAll(t *testing.T) {
  116. defer prepareTestEnv(t)()
  117. req := NewRequestf(t, "GET", "/api/v1/orgs")
  118. resp := MakeRequest(t, req, http.StatusOK)
  119. var apiOrgList []*api.Organization
  120. DecodeJSON(t, resp, &apiOrgList)
  121. assert.Len(t, apiOrgList, 7)
  122. assert.Equal(t, "org25", apiOrgList[0].FullName)
  123. assert.Equal(t, "public", apiOrgList[0].Visibility)
  124. }