access_tokens_test.go 4.6 KB

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