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

token_test.go 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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. tokens, err = ListAccessTokens(2)
  48. assert.NoError(t, err)
  49. assert.Len(t, tokens, 1)
  50. assert.Equal(t, int64(2), tokens[0].UID)
  51. assert.Equal(t, "Token A", tokens[0].Name)
  52. tokens, err = ListAccessTokens(100)
  53. assert.NoError(t, err)
  54. assert.Empty(t, tokens)
  55. }
  56. func TestUpdateAccessToken(t *testing.T) {
  57. assert.NoError(t, PrepareTestDatabase())
  58. token, err := GetAccessTokenBySHA("hash2")
  59. assert.NoError(t, err)
  60. token.Name = "Token Z"
  61. assert.NoError(t, UpdateAccessToken(token))
  62. AssertExistsAndLoadBean(t, token)
  63. }
  64. func TestDeleteAccessTokenByID(t *testing.T) {
  65. assert.NoError(t, PrepareTestDatabase())
  66. token, err := GetAccessTokenBySHA("hash2")
  67. assert.NoError(t, err)
  68. assert.Equal(t, int64(1), token.UID)
  69. assert.NoError(t, DeleteAccessTokenByID(token.ID, 1))
  70. AssertNotExistsBean(t, token)
  71. err = DeleteAccessTokenByID(100, 100)
  72. assert.Error(t, err)
  73. assert.True(t, IsErrAccessTokenNotExist(err))
  74. }