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

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