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.

130 lines
3.5 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2016 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 models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestNewAccessToken(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. token := &AccessToken{
  12. UID: 3,
  13. Name: "Token C",
  14. }
  15. assert.NoError(t, NewAccessToken(token))
  16. AssertExistsAndLoadBean(t, token)
  17. invalidToken := &AccessToken{
  18. ID: token.ID, // duplicate
  19. UID: 2,
  20. Name: "Token F",
  21. }
  22. assert.Error(t, NewAccessToken(invalidToken))
  23. }
  24. func TestAccessTokenByNameExists(t *testing.T) {
  25. name := "Token Gitea"
  26. assert.NoError(t, PrepareTestDatabase())
  27. token := &AccessToken{
  28. UID: 3,
  29. Name: name,
  30. }
  31. // Check to make sure it doesn't exists already
  32. exist, err := AccessTokenByNameExists(token)
  33. assert.NoError(t, err)
  34. assert.False(t, exist)
  35. // Save it to the database
  36. assert.NoError(t, NewAccessToken(token))
  37. AssertExistsAndLoadBean(t, token)
  38. // This token must be found by name in the DB now
  39. exist, err = AccessTokenByNameExists(token)
  40. assert.NoError(t, err)
  41. assert.True(t, exist)
  42. user4Token := &AccessToken{
  43. UID: 4,
  44. Name: name,
  45. }
  46. // Name matches but different user ID, this shouldn't exists in the
  47. // database
  48. exist, err = AccessTokenByNameExists(user4Token)
  49. assert.NoError(t, err)
  50. assert.False(t, exist)
  51. }
  52. func TestGetAccessTokenBySHA(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. token, err := GetAccessTokenBySHA("d2c6c1ba3890b309189a8e618c72a162e4efbf36")
  55. assert.NoError(t, err)
  56. assert.Equal(t, int64(1), token.UID)
  57. assert.Equal(t, "Token A", token.Name)
  58. assert.Equal(t, "2b3668e11cb82d3af8c6e4524fc7841297668f5008d1626f0ad3417e9fa39af84c268248b78c481daa7e5dc437784003494f", token.TokenHash)
  59. assert.Equal(t, "e4efbf36", token.TokenLastEight)
  60. _, err = GetAccessTokenBySHA("notahash")
  61. assert.Error(t, err)
  62. assert.True(t, IsErrAccessTokenNotExist(err))
  63. _, err = GetAccessTokenBySHA("")
  64. assert.Error(t, err)
  65. assert.True(t, IsErrAccessTokenEmpty(err))
  66. }
  67. func TestListAccessTokens(t *testing.T) {
  68. assert.NoError(t, PrepareTestDatabase())
  69. tokens, err := ListAccessTokens(1, ListOptions{})
  70. assert.NoError(t, err)
  71. if assert.Len(t, tokens, 2) {
  72. assert.Equal(t, int64(1), tokens[0].UID)
  73. assert.Equal(t, int64(1), tokens[1].UID)
  74. assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
  75. assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
  76. }
  77. tokens, err = ListAccessTokens(2, ListOptions{})
  78. assert.NoError(t, err)
  79. if assert.Len(t, tokens, 1) {
  80. assert.Equal(t, int64(2), tokens[0].UID)
  81. assert.Equal(t, "Token A", tokens[0].Name)
  82. }
  83. tokens, err = ListAccessTokens(100, ListOptions{})
  84. assert.NoError(t, err)
  85. assert.Empty(t, tokens)
  86. }
  87. func TestUpdateAccessToken(t *testing.T) {
  88. assert.NoError(t, PrepareTestDatabase())
  89. token, err := GetAccessTokenBySHA("4c6f36e6cf498e2a448662f915d932c09c5a146c")
  90. assert.NoError(t, err)
  91. token.Name = "Token Z"
  92. assert.NoError(t, UpdateAccessToken(token))
  93. AssertExistsAndLoadBean(t, token)
  94. }
  95. func TestDeleteAccessTokenByID(t *testing.T) {
  96. assert.NoError(t, PrepareTestDatabase())
  97. token, err := GetAccessTokenBySHA("4c6f36e6cf498e2a448662f915d932c09c5a146c")
  98. assert.NoError(t, err)
  99. assert.Equal(t, int64(1), token.UID)
  100. assert.NoError(t, DeleteAccessTokenByID(token.ID, 1))
  101. AssertNotExistsBean(t, token)
  102. err = DeleteAccessTokenByID(100, 100)
  103. assert.Error(t, err)
  104. assert.True(t, IsErrAccessTokenNotExist(err))
  105. }