Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

token_test.go 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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("hash1")
  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, "hash1", token.Sha1)
  31. token, err = GetAccessTokenBySHA("notahash")
  32. assert.Error(t, err)
  33. assert.True(t, IsErrAccessTokenNotExist(err))
  34. token, err = GetAccessTokenBySHA("")
  35. assert.Error(t, err)
  36. assert.True(t, IsErrAccessTokenEmpty(err))
  37. }
  38. func TestListAccessTokens(t *testing.T) {
  39. assert.NoError(t, PrepareTestDatabase())
  40. tokens, err := ListAccessTokens(1)
  41. assert.NoError(t, err)
  42. if assert.Len(t, tokens, 2) {
  43. assert.Equal(t, int64(1), tokens[0].UID)
  44. assert.Equal(t, int64(1), tokens[1].UID)
  45. assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
  46. assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
  47. }
  48. tokens, err = ListAccessTokens(2)
  49. assert.NoError(t, err)
  50. if assert.Len(t, tokens, 1) {
  51. assert.Equal(t, int64(2), tokens[0].UID)
  52. assert.Equal(t, "Token A", tokens[0].Name)
  53. }
  54. tokens, err = ListAccessTokens(100)
  55. assert.NoError(t, err)
  56. assert.Empty(t, tokens)
  57. }
  58. func TestUpdateAccessToken(t *testing.T) {
  59. assert.NoError(t, PrepareTestDatabase())
  60. token, err := GetAccessTokenBySHA("hash2")
  61. assert.NoError(t, err)
  62. token.Name = "Token Z"
  63. assert.NoError(t, UpdateAccessToken(token))
  64. AssertExistsAndLoadBean(t, token)
  65. }
  66. func TestDeleteAccessTokenByID(t *testing.T) {
  67. assert.NoError(t, PrepareTestDatabase())
  68. token, err := GetAccessTokenBySHA("hash2")
  69. assert.NoError(t, err)
  70. assert.Equal(t, int64(1), token.UID)
  71. assert.NoError(t, DeleteAccessTokenByID(token.ID, 1))
  72. AssertNotExistsBean(t, token)
  73. err = DeleteAccessTokenByID(100, 100)
  74. assert.Error(t, err)
  75. assert.True(t, IsErrAccessTokenNotExist(err))
  76. }