setup_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package setup
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/mock"
  8. "github.com/usememos/memos/api"
  9. )
  10. func TestSetupServiceMakeSureHostUserNotExists(t *testing.T) {
  11. cc := map[string]struct {
  12. setupStore func(*storeMock)
  13. expectedErr string
  14. }{
  15. "failed to get list": {
  16. setupStore: func(m *storeMock) {
  17. hostUserType := api.Host
  18. m.
  19. On("FindUserList", mock.Anything, &api.UserFind{
  20. Role: &hostUserType,
  21. }).
  22. Return(nil, errors.New("fake error"))
  23. },
  24. expectedErr: "find user list: fake error",
  25. },
  26. "success, not empty": {
  27. setupStore: func(m *storeMock) {
  28. hostUserType := api.Host
  29. m.
  30. On("FindUserList", mock.Anything, &api.UserFind{
  31. Role: &hostUserType,
  32. }).
  33. Return([]*api.User{
  34. {},
  35. }, nil)
  36. },
  37. expectedErr: "host user already exists",
  38. },
  39. "success, empty": {
  40. setupStore: func(m *storeMock) {
  41. hostUserType := api.Host
  42. m.
  43. On("FindUserList", mock.Anything, &api.UserFind{
  44. Role: &hostUserType,
  45. }).
  46. Return(nil, nil)
  47. },
  48. },
  49. }
  50. for n, c := range cc {
  51. c := c
  52. t.Run(n, func(t *testing.T) {
  53. sm := newStoreMock(t)
  54. if c.setupStore != nil {
  55. c.setupStore(sm)
  56. }
  57. srv := setupService{store: sm}
  58. err := srv.makeSureHostUserNotExists(context.Background())
  59. if c.expectedErr == "" {
  60. assert.NoError(t, err)
  61. } else {
  62. assert.EqualError(t, err, c.expectedErr)
  63. }
  64. })
  65. }
  66. }
  67. func TestSetupServiceCreateUser(t *testing.T) {
  68. expectedCreated := &api.UserCreate{
  69. Username: "demohero",
  70. Role: api.Host,
  71. Nickname: "demohero",
  72. Password: "123456",
  73. }
  74. userCreateMatcher := mock.MatchedBy(func(arg *api.UserCreate) bool {
  75. return arg.Username == expectedCreated.Username &&
  76. arg.Role == expectedCreated.Role &&
  77. arg.Nickname == expectedCreated.Nickname &&
  78. arg.Password == expectedCreated.Password &&
  79. arg.PasswordHash != ""
  80. })
  81. cc := map[string]struct {
  82. setupStore func(*storeMock)
  83. hostUsername, hostPassword string
  84. expectedErr string
  85. }{
  86. `username == "", password == ""`: {
  87. expectedErr: "validate: username is too short, minimum length is 3",
  88. },
  89. `username == "", password != ""`: {
  90. hostPassword: expectedCreated.Password,
  91. expectedErr: "validate: username is too short, minimum length is 3",
  92. },
  93. `username != "", password == ""`: {
  94. hostUsername: expectedCreated.Username,
  95. expectedErr: "validate: password is too short, minimum length is 6",
  96. },
  97. "failed to create": {
  98. setupStore: func(m *storeMock) {
  99. m.
  100. On("CreateUser", mock.Anything, userCreateMatcher).
  101. Return(nil, errors.New("fake error"))
  102. },
  103. hostUsername: expectedCreated.Username,
  104. hostPassword: expectedCreated.Password,
  105. expectedErr: "create user: fake error",
  106. },
  107. "success": {
  108. setupStore: func(m *storeMock) {
  109. m.
  110. On("CreateUser", mock.Anything, userCreateMatcher).
  111. Return(nil, nil)
  112. },
  113. hostUsername: expectedCreated.Username,
  114. hostPassword: expectedCreated.Password,
  115. },
  116. }
  117. for n, c := range cc {
  118. c := c
  119. t.Run(n, func(t *testing.T) {
  120. sm := newStoreMock(t)
  121. if c.setupStore != nil {
  122. c.setupStore(sm)
  123. }
  124. srv := setupService{store: sm}
  125. err := srv.createUser(context.Background(), c.hostUsername, c.hostPassword)
  126. if c.expectedErr == "" {
  127. assert.NoError(t, err)
  128. } else {
  129. assert.EqualError(t, err, c.expectedErr)
  130. }
  131. })
  132. }
  133. }
  134. type storeMock struct {
  135. mock.Mock
  136. }
  137. func (m *storeMock) FindUserList(ctx context.Context, find *api.UserFind) ([]*api.User, error) {
  138. ret := m.Called(ctx, find)
  139. var u []*api.User
  140. ret1 := ret.Get(0)
  141. if ret1 != nil {
  142. u = ret1.([]*api.User)
  143. }
  144. return u, ret.Error(1)
  145. }
  146. func (m *storeMock) CreateUser(ctx context.Context, create *api.UserCreate) (*api.User, error) {
  147. ret := m.Called(ctx, create)
  148. var u *api.User
  149. ret1 := ret.Get(0)
  150. if ret1 != nil {
  151. u = ret1.(*api.User)
  152. }
  153. return u, ret.Error(1)
  154. }
  155. func newStoreMock(t *testing.T) *storeMock {
  156. m := &storeMock{}
  157. m.Mock.Test(t)
  158. t.Cleanup(func() { m.AssertExpectations(t) })
  159. return m
  160. }