本站源代码
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.

95 lines
2.6KB

  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 TestGetAccessTokenBySHA(t *testing.T) {
  25. assert.NoError(t, PrepareTestDatabase())
  26. token, err := GetAccessTokenBySHA("d2c6c1ba3890b309189a8e618c72a162e4efbf36")
  27. assert.NoError(t, err)
  28. assert.Equal(t, int64(1), token.UID)
  29. assert.Equal(t, "Token A", token.Name)
  30. assert.Equal(t, "2b3668e11cb82d3af8c6e4524fc7841297668f5008d1626f0ad3417e9fa39af84c268248b78c481daa7e5dc437784003494f", token.TokenHash)
  31. assert.Equal(t, "e4efbf36", token.TokenLastEight)
  32. _, err = GetAccessTokenBySHA("notahash")
  33. assert.Error(t, err)
  34. assert.True(t, IsErrAccessTokenNotExist(err))
  35. _, err = GetAccessTokenBySHA("")
  36. assert.Error(t, err)
  37. assert.True(t, IsErrAccessTokenEmpty(err))
  38. }
  39. func TestListAccessTokens(t *testing.T) {
  40. assert.NoError(t, PrepareTestDatabase())
  41. tokens, err := ListAccessTokens(1)
  42. assert.NoError(t, err)
  43. if assert.Len(t, tokens, 2) {
  44. assert.Equal(t, int64(1), tokens[0].UID)
  45. assert.Equal(t, int64(1), tokens[1].UID)
  46. assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
  47. assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
  48. }
  49. tokens, err = ListAccessTokens(2)
  50. assert.NoError(t, err)
  51. if assert.Len(t, tokens, 1) {
  52. assert.Equal(t, int64(2), tokens[0].UID)
  53. assert.Equal(t, "Token A", tokens[0].Name)
  54. }
  55. tokens, err = ListAccessTokens(100)
  56. assert.NoError(t, err)
  57. assert.Empty(t, tokens)
  58. }
  59. func TestUpdateAccessToken(t *testing.T) {
  60. assert.NoError(t, PrepareTestDatabase())
  61. token, err := GetAccessTokenBySHA("4c6f36e6cf498e2a448662f915d932c09c5a146c")
  62. assert.NoError(t, err)
  63. token.Name = "Token Z"
  64. assert.NoError(t, UpdateAccessToken(token))
  65. AssertExistsAndLoadBean(t, token)
  66. }
  67. func TestDeleteAccessTokenByID(t *testing.T) {
  68. assert.NoError(t, PrepareTestDatabase())
  69. token, err := GetAccessTokenBySHA("4c6f36e6cf498e2a448662f915d932c09c5a146c")
  70. assert.NoError(t, err)
  71. assert.Equal(t, int64(1), token.UID)
  72. assert.NoError(t, DeleteAccessTokenByID(token.ID, 1))
  73. AssertNotExistsBean(t, token)
  74. err = DeleteAccessTokenByID(100, 100)
  75. assert.Error(t, err)
  76. assert.True(t, IsErrAccessTokenNotExist(err))
  77. }
上海开阖软件有限公司 沪ICP备12045867号-1