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.

86 lines
2.6 KiB

  1. // Copyright 2019 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. api "code.gitea.io/gitea/modules/structs"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPIAdminOrgCreate(t *testing.T) {
  15. onGiteaRun(t, func(*testing.T, *url.URL) {
  16. session := loginUser(t, "user1")
  17. token := getTokenForLoggedInUser(t, session)
  18. var org = api.CreateOrgOption{
  19. UserName: "user2_org",
  20. FullName: "User2's organization",
  21. Description: "This organization created by admin for user2",
  22. Website: "https://try.gitea.io",
  23. Location: "Shanghai",
  24. Visibility: "private",
  25. }
  26. req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
  27. resp := session.MakeRequest(t, req, http.StatusCreated)
  28. var apiOrg api.Organization
  29. DecodeJSON(t, resp, &apiOrg)
  30. assert.Equal(t, org.UserName, apiOrg.UserName)
  31. assert.Equal(t, org.FullName, apiOrg.FullName)
  32. assert.Equal(t, org.Description, apiOrg.Description)
  33. assert.Equal(t, org.Website, apiOrg.Website)
  34. assert.Equal(t, org.Location, apiOrg.Location)
  35. assert.Equal(t, org.Visibility, apiOrg.Visibility)
  36. models.AssertExistsAndLoadBean(t, &models.User{
  37. Name: org.UserName,
  38. LowerName: strings.ToLower(org.UserName),
  39. FullName: org.FullName,
  40. })
  41. })
  42. }
  43. func TestAPIAdminOrgCreateBadVisibility(t *testing.T) {
  44. onGiteaRun(t, func(*testing.T, *url.URL) {
  45. session := loginUser(t, "user1")
  46. token := getTokenForLoggedInUser(t, session)
  47. var org = api.CreateOrgOption{
  48. UserName: "user2_org",
  49. FullName: "User2's organization",
  50. Description: "This organization created by admin for user2",
  51. Website: "https://try.gitea.io",
  52. Location: "Shanghai",
  53. Visibility: "notvalid",
  54. }
  55. req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
  56. session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  57. })
  58. }
  59. func TestAPIAdminOrgCreateNotAdmin(t *testing.T) {
  60. defer prepareTestEnv(t)()
  61. nonAdminUsername := "user2"
  62. session := loginUser(t, nonAdminUsername)
  63. token := getTokenForLoggedInUser(t, session)
  64. var org = api.CreateOrgOption{
  65. UserName: "user2_org",
  66. FullName: "User2's organization",
  67. Description: "This organization created by admin for user2",
  68. Website: "https://try.gitea.io",
  69. Location: "Shanghai",
  70. Visibility: "public",
  71. }
  72. req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
  73. session.MakeRequest(t, req, http.StatusForbidden)
  74. }