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.8KB

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