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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. testAPIDeleteOAuth2Application(t)
  18. }
  19. func testAPICreateOAuth2Application(t *testing.T) {
  20. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  21. appBody := api.CreateOAuth2ApplicationOptions{
  22. Name: "test-app-1",
  23. RedirectURIs: []string{
  24. "http://www.google.com",
  25. },
  26. }
  27. req := NewRequestWithJSON(t, "POST", "/api/v1/user/applications/oauth2", &appBody)
  28. req = AddBasicAuthHeader(req, user.Name)
  29. resp := MakeRequest(t, req, http.StatusCreated)
  30. var createdApp *api.OAuth2Application
  31. DecodeJSON(t, resp, &createdApp)
  32. assert.EqualValues(t, appBody.Name, createdApp.Name)
  33. assert.Len(t, createdApp.ClientSecret, 44)
  34. assert.Len(t, createdApp.ClientID, 36)
  35. assert.NotEmpty(t, createdApp.Created)
  36. assert.EqualValues(t, appBody.RedirectURIs[0], createdApp.RedirectURIs[0])
  37. models.AssertExistsAndLoadBean(t, &models.OAuth2Application{UID: user.ID, Name: createdApp.Name})
  38. }
  39. func testAPIListOAuth2Applications(t *testing.T) {
  40. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  41. session := loginUser(t, user.Name)
  42. token := getTokenForLoggedInUser(t, session)
  43. existApp := models.AssertExistsAndLoadBean(t, &models.OAuth2Application{
  44. UID: user.ID,
  45. Name: "test-app-1",
  46. RedirectURIs: []string{
  47. "http://www.google.com",
  48. },
  49. }).(*models.OAuth2Application)
  50. urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2?token=%s", token)
  51. req := NewRequest(t, "GET", urlStr)
  52. resp := session.MakeRequest(t, req, http.StatusOK)
  53. var appList api.OAuth2ApplicationList
  54. DecodeJSON(t, resp, &appList)
  55. expectedApp := appList[0]
  56. assert.EqualValues(t, existApp.Name, expectedApp.Name)
  57. assert.EqualValues(t, existApp.ClientID, expectedApp.ClientID)
  58. assert.Len(t, expectedApp.ClientID, 36)
  59. assert.Empty(t, expectedApp.ClientSecret)
  60. assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0])
  61. models.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
  62. }
  63. func testAPIDeleteOAuth2Application(t *testing.T) {
  64. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  65. session := loginUser(t, user.Name)
  66. token := getTokenForLoggedInUser(t, session)
  67. oldApp := models.AssertExistsAndLoadBean(t, &models.OAuth2Application{
  68. UID: user.ID,
  69. Name: "test-app-1",
  70. RedirectURIs: []string{
  71. "http://www.google.com",
  72. },
  73. }).(*models.OAuth2Application)
  74. urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", oldApp.ID, token)
  75. req := NewRequest(t, "DELETE", urlStr)
  76. session.MakeRequest(t, req, http.StatusNoContent)
  77. models.AssertNotExistsBean(t, &models.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name})
  78. }