access_tokens_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2020 The Gogs 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 db
  5. import (
  6. "testing"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. "github.com/stretchr/testify/assert"
  10. "gogs.io/gogs/internal/errutil"
  11. )
  12. func Test_accessTokens(t *testing.T) {
  13. if testing.Short() {
  14. t.Skip()
  15. }
  16. t.Parallel()
  17. tables := []interface{}{new(AccessToken)}
  18. db := &accessTokens{
  19. DB: initTestDB(t, "accessTokens", tables...),
  20. }
  21. for _, tc := range []struct {
  22. name string
  23. test func(*testing.T, *accessTokens)
  24. }{
  25. {"Create", test_accessTokens_Create},
  26. {"DeleteByID", test_accessTokens_DeleteByID},
  27. {"GetBySHA", test_accessTokens_GetBySHA},
  28. {"List", test_accessTokens_List},
  29. {"Save", test_accessTokens_Save},
  30. } {
  31. t.Run(tc.name, func(t *testing.T) {
  32. t.Cleanup(func() {
  33. err := clearTables(db.DB, tables...)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. })
  38. tc.test(t, db)
  39. })
  40. }
  41. }
  42. func test_accessTokens_Create(t *testing.T, db *accessTokens) {
  43. // Create first access token with name "Test"
  44. token, err := db.Create(1, "Test")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. assert.Equal(t, int64(1), token.UserID)
  49. assert.Equal(t, "Test", token.Name)
  50. assert.Equal(t, 40, len(token.Sha1), "sha1 length")
  51. // Get it back and check the Created field
  52. token, err = db.GetBySHA(token.Sha1)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), token.Created.Format(time.RFC3339))
  57. // Try create second access token with same name should fail
  58. _, err = db.Create(token.UserID, token.Name)
  59. expErr := ErrAccessTokenAlreadyExist{args: errutil.Args{"userID": token.UserID, "name": token.Name}}
  60. assert.Equal(t, expErr, err)
  61. }
  62. func test_accessTokens_DeleteByID(t *testing.T, db *accessTokens) {
  63. // Create an access token with name "Test"
  64. token, err := db.Create(1, "Test")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. // Delete a token with mismatched user ID is noop
  69. err = db.DeleteByID(2, token.ID)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. // We should be able to get it back
  74. _, err = db.GetBySHA(token.Sha1)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. _, err = db.GetBySHA(token.Sha1)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. // Now delete this token with correct user ID
  83. err = db.DeleteByID(token.UserID, token.ID)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. // We should get token not found error
  88. _, err = db.GetBySHA(token.Sha1)
  89. expErr := ErrAccessTokenNotExist{args: errutil.Args{"sha": token.Sha1}}
  90. assert.Equal(t, expErr, err)
  91. }
  92. func test_accessTokens_GetBySHA(t *testing.T, db *accessTokens) {
  93. // Create an access token with name "Test"
  94. token, err := db.Create(1, "Test")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. // We should be able to get it back
  99. _, err = db.GetBySHA(token.Sha1)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. // Try to get a non-existent token
  104. _, err = db.GetBySHA("bad_sha")
  105. expErr := ErrAccessTokenNotExist{args: errutil.Args{"sha": "bad_sha"}}
  106. assert.Equal(t, expErr, err)
  107. }
  108. func test_accessTokens_List(t *testing.T, db *accessTokens) {
  109. // Create two access tokens for user 1
  110. _, err := db.Create(1, "user1_1")
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. _, err = db.Create(1, "user1_2")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. // Create one access token for user 2
  119. _, err = db.Create(2, "user2_1")
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. // List all access tokens for user 1
  124. tokens, err := db.List(1)
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. assert.Equal(t, 2, len(tokens), "number of tokens")
  129. assert.Equal(t, int64(1), tokens[0].UserID)
  130. assert.Equal(t, "user1_1", tokens[0].Name)
  131. assert.Equal(t, int64(1), tokens[1].UserID)
  132. assert.Equal(t, "user1_2", tokens[1].Name)
  133. }
  134. func test_accessTokens_Save(t *testing.T, db *accessTokens) {
  135. // Create an access token with name "Test"
  136. token, err := db.Create(1, "Test")
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. // Updated field is zero now
  141. assert.True(t, token.Updated.IsZero())
  142. err = db.Save(token)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. // Get back from DB should have Updated set
  147. token, err = db.GetBySHA(token.Sha1)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), token.Updated.Format(time.RFC3339))
  152. }