2
0

login_source_files_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "github.com/stretchr/testify/assert"
  8. "gogs.io/gogs/internal/errutil"
  9. )
  10. func Test_loginSourceFiles_GetByID(t *testing.T) {
  11. store := &loginSourceFiles{
  12. sources: []*LoginSource{
  13. {ID: 101},
  14. },
  15. }
  16. t.Run("source does not exist", func(t *testing.T) {
  17. _, err := store.GetByID(1)
  18. expErr := ErrLoginSourceNotExist{args: errutil.Args{"id": int64(1)}}
  19. assert.Equal(t, expErr, err)
  20. })
  21. t.Run("source exists", func(t *testing.T) {
  22. source, err := store.GetByID(101)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. assert.Equal(t, int64(101), source.ID)
  27. })
  28. }
  29. func Test_loginSourceFiles_Len(t *testing.T) {
  30. store := &loginSourceFiles{
  31. sources: []*LoginSource{
  32. {ID: 101},
  33. },
  34. }
  35. assert.Equal(t, 1, store.Len())
  36. }
  37. func Test_loginSourceFiles_List(t *testing.T) {
  38. store := &loginSourceFiles{
  39. sources: []*LoginSource{
  40. {ID: 101, IsActived: true},
  41. {ID: 102, IsActived: false},
  42. },
  43. }
  44. t.Run("list all sources", func(t *testing.T) {
  45. sources := store.List(ListLoginSourceOpts{})
  46. assert.Equal(t, 2, len(sources), "number of sources")
  47. })
  48. t.Run("list only activated sources", func(t *testing.T) {
  49. sources := store.List(ListLoginSourceOpts{OnlyActivated: true})
  50. assert.Equal(t, 1, len(sources), "number of sources")
  51. assert.Equal(t, int64(101), sources[0].ID)
  52. })
  53. }
  54. func Test_loginSourceFiles_Update(t *testing.T) {
  55. store := &loginSourceFiles{
  56. sources: []*LoginSource{
  57. {ID: 101, IsActived: true, IsDefault: true},
  58. {ID: 102, IsActived: false},
  59. },
  60. }
  61. source102 := &LoginSource{
  62. ID: 102,
  63. IsActived: true,
  64. IsDefault: true,
  65. }
  66. store.Update(source102)
  67. assert.False(t, store.sources[0].IsDefault)
  68. assert.True(t, store.sources[1].IsActived)
  69. assert.True(t, store.sources[1].IsDefault)
  70. }