access_tokens_test.go 4.7 KB

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