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.

api_oauth2_apps_test.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2020 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.package models
  4. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestOAuth2Application(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. testAPICreateOAuth2Application(t)
  16. testAPIListOAuth2Applications(t)
  17. testAPIGetOAuth2Application(t)
  18. testAPIUpdateOAuth2Application(t)
  19. testAPIDeleteOAuth2Application(t)
  20. }
  21. func testAPICreateOAuth2Application(t *testing.T) {
  22. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  23. appBody := api.CreateOAuth2ApplicationOptions{
  24. Name: "test-app-1",
  25. RedirectURIs: []string{
  26. "http://www.google.com",
  27. },
  28. }
  29. req := NewRequestWithJSON(t, "POST", "/api/v1/user/applications/oauth2", &appBody)
  30. req = AddBasicAuthHeader(req, user.Name)
  31. resp := MakeRequest(t, req, http.StatusCreated)
  32. var createdApp *api.OAuth2Application
  33. DecodeJSON(t, resp, &createdApp)
  34. assert.EqualValues(t, appBody.Name, createdApp.Name)
  35. assert.Len(t, createdApp.ClientSecret, 44)
  36. assert.Len(t, createdApp.ClientID, 36)
  37. assert.NotEmpty(t, createdApp.Created)
  38. assert.EqualValues(t, appBody.RedirectURIs[0], createdApp.RedirectURIs[0])
  39. models.AssertExistsAndLoadBean(t, &models.OAuth2Application{UID: user.ID, Name: createdApp.Name})
  40. }
  41. func testAPIListOAuth2Applications(t *testing.T) {
  42. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  43. session := loginUser(t, user.Name)
  44. token := getTokenForLoggedInUser(t, session)
  45. existApp := models.AssertExistsAndLoadBean(t, &models.OAuth2Application{
  46. UID: user.ID,
  47. Name: "test-app-1",
  48. RedirectURIs: []string{
  49. "http://www.google.com",
  50. },
  51. }).(*models.OAuth2Application)
  52. urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2?token=%s", token)
  53. req := NewRequest(t, "GET", urlStr)
  54. resp := session.MakeRequest(t, req, http.StatusOK)
  55. var appList api.OAuth2ApplicationList
  56. DecodeJSON(t, resp, &appList)
  57. expectedApp := appList[0]
  58. assert.EqualValues(t, existApp.Name, expectedApp.Name)
  59. assert.EqualValues(t, existApp.ClientID, expectedApp.ClientID)
  60. assert.Len(t, expectedApp.ClientID, 36)
  61. assert.Empty(t, expectedApp.ClientSecret)
  62. assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0])
  63. models.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
  64. }
  65. func testAPIDeleteOAuth2Application(t *testing.T) {
  66. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  67. session := loginUser(t, user.Name)
  68. token := getTokenForLoggedInUser(t, session)
  69. oldApp := models.AssertExistsAndLoadBean(t, &models.OAuth2Application{
  70. UID: user.ID,
  71. Name: "test-app-1",
  72. }).(*models.OAuth2Application)
  73. urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", oldApp.ID, token)
  74. req := NewRequest(t, "DELETE", urlStr)
  75. session.MakeRequest(t, req, http.StatusNoContent)
  76. models.AssertNotExistsBean(t, &models.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name})
  77. // Delete again will return not found
  78. req = NewRequest(t, "DELETE", urlStr)
  79. session.MakeRequest(t, req, http.StatusNotFound)
  80. }
  81. func testAPIGetOAuth2Application(t *testing.T) {
  82. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  83. session := loginUser(t, user.Name)
  84. token := getTokenForLoggedInUser(t, session)
  85. existApp := models.AssertExistsAndLoadBean(t, &models.OAuth2Application{
  86. UID: user.ID,
  87. Name: "test-app-1",
  88. RedirectURIs: []string{
  89. "http://www.google.com",
  90. },
  91. }).(*models.OAuth2Application)
  92. urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", existApp.ID, token)
  93. req := NewRequest(t, "GET", urlStr)
  94. resp := session.MakeRequest(t, req, http.StatusOK)
  95. var app api.OAuth2Application
  96. DecodeJSON(t, resp, &app)
  97. expectedApp := app
  98. assert.EqualValues(t, existApp.Name, expectedApp.Name)
  99. assert.EqualValues(t, existApp.ClientID, expectedApp.ClientID)
  100. assert.Len(t, expectedApp.ClientID, 36)
  101. assert.Empty(t, expectedApp.ClientSecret)
  102. assert.EqualValues(t, len(expectedApp.RedirectURIs), 1)
  103. assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0])
  104. models.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
  105. }
  106. func testAPIUpdateOAuth2Application(t *testing.T) {
  107. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  108. existApp := models.AssertExistsAndLoadBean(t, &models.OAuth2Application{
  109. UID: user.ID,
  110. Name: "test-app-1",
  111. RedirectURIs: []string{
  112. "http://www.google.com",
  113. },
  114. }).(*models.OAuth2Application)
  115. appBody := api.CreateOAuth2ApplicationOptions{
  116. Name: "test-app-1",
  117. RedirectURIs: []string{
  118. "http://www.google.com/",
  119. "http://www.github.com/",
  120. },
  121. }
  122. urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d", existApp.ID)
  123. req := NewRequestWithJSON(t, "PATCH", urlStr, &appBody)
  124. req = AddBasicAuthHeader(req, user.Name)
  125. resp := MakeRequest(t, req, http.StatusOK)
  126. var app api.OAuth2Application
  127. DecodeJSON(t, resp, &app)
  128. expectedApp := app
  129. assert.EqualValues(t, len(expectedApp.RedirectURIs), 2)
  130. assert.EqualValues(t, expectedApp.RedirectURIs[0], appBody.RedirectURIs[0])
  131. assert.EqualValues(t, expectedApp.RedirectURIs[1], appBody.RedirectURIs[1])
  132. models.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
  133. }