Migrate TermsOfService and UserTermsOfService stores to sync by default (#11282)

This commit is contained in:
Jesús Espino
2019-07-02 16:22:20 +02:00
committed by GitHub
parent 1050aacb42
commit 76ac7d8ef0
9 changed files with 209 additions and 252 deletions

View File

@@ -17,29 +17,13 @@ func (a *App) CreateTermsOfService(text, userId string) (*model.TermsOfService,
return nil, err
}
result := <-a.Srv.Store.TermsOfService().Save(termsOfService)
if result.Err != nil {
return nil, result.Err
}
termsOfService = result.Data.(*model.TermsOfService)
return termsOfService, nil
return a.Srv.Store.TermsOfService().Save(termsOfService)
}
func (a *App) GetLatestTermsOfService() (*model.TermsOfService, *model.AppError) {
if result := <-a.Srv.Store.TermsOfService().GetLatest(true); result.Err != nil {
return nil, result.Err
} else {
termsOfService := result.Data.(*model.TermsOfService)
return termsOfService, nil
}
return a.Srv.Store.TermsOfService().GetLatest(true)
}
func (a *App) GetTermsOfService(id string) (*model.TermsOfService, *model.AppError) {
if result := <-a.Srv.Store.TermsOfService().Get(id, true); result.Err != nil {
return nil, result.Err
} else {
termsOfService := result.Data.(*model.TermsOfService)
return termsOfService, nil
}
return a.Srv.Store.TermsOfService().Get(id, true)
}

View File

@@ -6,11 +6,7 @@ package app
import "github.com/mattermost/mattermost-server/model"
func (a *App) GetUserTermsOfService(userId string) (*model.UserTermsOfService, *model.AppError) {
if result := <-a.Srv.Store.UserTermsOfService().GetByUser(userId); result.Err != nil {
return nil, result.Err
} else {
return result.Data.(*model.UserTermsOfService), nil
}
return a.Srv.Store.UserTermsOfService().GetByUser(userId)
}
func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted bool) *model.AppError {
@@ -20,12 +16,12 @@ func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted b
TermsOfServiceId: termsOfServiceId,
}
if result := <-a.Srv.Store.UserTermsOfService().Save(userTermsOfService); result.Err != nil {
return result.Err
if _, err := a.Srv.Store.UserTermsOfService().Save(userTermsOfService); err != nil {
return err
}
} else {
if result := <-a.Srv.Store.UserTermsOfService().Delete(userId, termsOfServiceId); result.Err != nil {
return result.Err
if err := a.Srv.Store.UserTermsOfService().Delete(userId, termsOfServiceId); err != nil {
return err
}
}

View File

@@ -40,107 +40,81 @@ func NewSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInt
func (s SqlTermsOfServiceStore) CreateIndexesIfNotExists() {
}
func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if len(termsOfService.Id) > 0 {
result.Err = model.NewAppError(
"SqlTermsOfServiceStore.Save",
"store.sql_terms_of_service_store.save.existing.app_error",
nil,
"id="+termsOfService.Id, http.StatusBadRequest,
)
return
func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
if len(termsOfService.Id) > 0 {
return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service_store.save.existing.app_error", nil, "id="+termsOfService.Id, http.StatusBadRequest)
}
termsOfService.PreSave()
if err := termsOfService.IsValid(); err != nil {
return nil, err
}
if err := s.GetMaster().Insert(termsOfService); err != nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service.save.app_error", nil, "terms_of_service_id="+termsOfService.Id+",err="+err.Error(), http.StatusInternalServerError)
}
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
if allowFromCache {
if termsOfServiceCache.Len() != 0 {
if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
return cacheItem.(*model.TermsOfService), nil
}
}
}
termsOfService.PreSave()
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
if result.Err = termsOfService.IsValid(); result.Err != nil {
return
var termsOfService *model.TermsOfService
err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1")
if err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound)
}
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
if err := s.GetMaster().Insert(termsOfService); err != nil {
result.Err = model.NewAppError(
"SqlTermsOfServiceStore.Save",
"store.sql_terms_of_service.save.app_error",
nil,
"terms_of_service_id="+termsOfService.Id+",err="+err.Error(),
http.StatusInternalServerError,
)
}
result.Data = termsOfService
if allowFromCache {
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
})
}
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if allowFromCache {
if termsOfServiceCache.Len() == 0 {
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
if allowFromCache {
if termsOfServiceCache.Len() != 0 {
if cacheItem, ok := termsOfServiceCache.Get(id); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
} else {
if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
result.Data = cacheItem.(*model.TermsOfService)
return
} else if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
return cacheItem.(*model.TermsOfService), nil
}
}
}
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
var termsOfService *model.TermsOfService
err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1")
if err != nil {
if err == sql.ErrNoRows {
result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound)
} else {
result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
} else {
result.Data = termsOfService
if allowFromCache {
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
}
}
})
}
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if allowFromCache {
if termsOfServiceCache.Len() == 0 {
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
} else {
if cacheItem, ok := termsOfServiceCache.Get(id); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
result.Data = cacheItem.(*model.TermsOfService)
return
} else if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
}
}
if obj, err := s.GetReplica().Get(model.TermsOfService{}, id); err != nil {
result.Err = model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} else if obj == nil {
result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
} else {
result.Data = obj.(*model.TermsOfService)
}
})
obj, err := s.GetReplica().Get(model.TermsOfService{}, id)
if err != nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
if obj == nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
}
return obj.(*model.TermsOfService), nil
}

View File

@@ -31,60 +31,43 @@ func (s SqlUserTermsOfServiceStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_user_terms_of_service_user_id", "UserTermsOfService", "UserId")
}
func (s SqlUserTermsOfServiceStore) GetByUser(userId string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var userTermsOfService *model.UserTermsOfService
func (s SqlUserTermsOfServiceStore) GetByUser(userId string) (*model.UserTermsOfService, *model.AppError) {
var userTermsOfService *model.UserTermsOfService
err := s.GetReplica().SelectOne(&userTermsOfService, "SELECT * FROM UserTermsOfService WHERE UserId = :userId", map[string]interface{}{"userId": userId})
if err != nil {
if err == sql.ErrNoRows {
result.Err = model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", nil, "", http.StatusNotFound)
} else {
result.Err = model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.app_error", nil, "", http.StatusInternalServerError)
}
} else {
result.Data = userTermsOfService
err := s.GetReplica().SelectOne(&userTermsOfService, "SELECT * FROM UserTermsOfService WHERE UserId = :userId", map[string]interface{}{"userId": userId})
if err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", nil, "", http.StatusNotFound)
}
})
return nil, model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.app_error", nil, "", http.StatusInternalServerError)
}
return userTermsOfService, nil
}
func (s SqlUserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
userTermsOfService.PreSave()
func (s SqlUserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError) {
userTermsOfService.PreSave()
if result.Err = userTermsOfService.IsValid(); result.Err != nil {
return
if err := userTermsOfService.IsValid(); err != nil {
return nil, err
}
c, err := s.GetMaster().Update(userTermsOfService)
if err != nil {
return nil, model.NewAppError("SqlUserTermsOfServiceStore.Save", "store.sql_user_terms_of_service.save.app_error", nil, "user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(), http.StatusInternalServerError)
}
if c == 0 {
if err := s.GetMaster().Insert(userTermsOfService); err != nil {
return nil, model.NewAppError("SqlUserTermsOfServiceStore.Save", "store.sql_user_terms_of_service.save.app_error", nil, "user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(), http.StatusInternalServerError)
}
}
if c, err := s.GetMaster().Update(userTermsOfService); err != nil {
result.Err = model.NewAppError(
"SqlUserTermsOfServiceStore.Save",
"store.sql_user_terms_of_service.save.app_error",
nil,
"user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(),
http.StatusInternalServerError,
)
} else if c == 0 {
if err := s.GetMaster().Insert(userTermsOfService); err != nil {
result.Err = model.NewAppError(
"SqlUserTermsOfServiceStore.Save",
"store.sql_user_terms_of_service.save.app_error",
nil,
"user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(),
http.StatusInternalServerError,
)
}
}
result.Data = userTermsOfService
})
return userTermsOfService, nil
}
func (s SqlUserTermsOfServiceStore) Delete(userId, termsOfServiceId string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if _, err := s.GetMaster().Exec("DELETE FROM UserTermsOfService WHERE UserId = :UserId AND TermsOfServiceId = :TermsOfServiceId", map[string]interface{}{"UserId": userId, "TermsOfServiceId": termsOfServiceId}); err != nil {
result.Err = model.NewAppError("SqlUserTermsOfServiceStore.Delete", "store.sql_user_terms_of_service.delete.app_error", nil, "userId="+userId+", termsOfServiceId="+termsOfServiceId, http.StatusInternalServerError)
return
}
})
func (s SqlUserTermsOfServiceStore) Delete(userId, termsOfServiceId string) *model.AppError {
if _, err := s.GetMaster().Exec("DELETE FROM UserTermsOfService WHERE UserId = :UserId AND TermsOfServiceId = :TermsOfServiceId", map[string]interface{}{"UserId": userId, "TermsOfServiceId": termsOfServiceId}); err != nil {
return model.NewAppError("SqlUserTermsOfServiceStore.Delete", "store.sql_user_terms_of_service.delete.app_error", nil, "userId="+userId+", termsOfServiceId="+termsOfServiceId, http.StatusInternalServerError)
}
return nil
}

View File

@@ -561,15 +561,15 @@ type SchemeStore interface {
}
type TermsOfServiceStore interface {
Save(termsOfService *model.TermsOfService) StoreChannel
GetLatest(allowFromCache bool) StoreChannel
Get(id string, allowFromCache bool) StoreChannel
Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError)
GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError)
Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError)
}
type UserTermsOfServiceStore interface {
GetByUser(userId string) StoreChannel
Save(userTermsOfService *model.UserTermsOfService) StoreChannel
Delete(userId, termsOfServiceId string) StoreChannel
GetByUser(userId string) (*model.UserTermsOfService, *model.AppError)
Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError)
Delete(userId, termsOfServiceId string) *model.AppError
}
type GroupStore interface {

View File

@@ -6,7 +6,6 @@ package mocks
import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model"
import store "github.com/mattermost/mattermost-server/store"
// TermsOfServiceStore is an autogenerated mock type for the TermsOfServiceStore type
type TermsOfServiceStore struct {
@@ -14,49 +13,76 @@ type TermsOfServiceStore struct {
}
// Get provides a mock function with given fields: id, allowFromCache
func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) store.StoreChannel {
func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
ret := _m.Called(id, allowFromCache)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string, bool) store.StoreChannel); ok {
var r0 *model.TermsOfService
if rf, ok := ret.Get(0).(func(string, bool) *model.TermsOfService); ok {
r0 = rf(id, allowFromCache)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.TermsOfService)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, bool) *model.AppError); ok {
r1 = rf(id, allowFromCache)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// GetLatest provides a mock function with given fields: allowFromCache
func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) store.StoreChannel {
func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
ret := _m.Called(allowFromCache)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(bool) store.StoreChannel); ok {
var r0 *model.TermsOfService
if rf, ok := ret.Get(0).(func(bool) *model.TermsOfService); ok {
r0 = rf(allowFromCache)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.TermsOfService)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(bool) *model.AppError); ok {
r1 = rf(allowFromCache)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// Save provides a mock function with given fields: termsOfService
func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) store.StoreChannel {
func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
ret := _m.Called(termsOfService)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(*model.TermsOfService) store.StoreChannel); ok {
var r0 *model.TermsOfService
if rf, ok := ret.Get(0).(func(*model.TermsOfService) *model.TermsOfService); ok {
r0 = rf(termsOfService)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.TermsOfService)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.TermsOfService) *model.AppError); ok {
r1 = rf(termsOfService)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}

View File

@@ -6,7 +6,6 @@ package mocks
import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model"
import store "github.com/mattermost/mattermost-server/store"
// UserTermsOfServiceStore is an autogenerated mock type for the UserTermsOfServiceStore type
type UserTermsOfServiceStore struct {
@@ -14,15 +13,15 @@ type UserTermsOfServiceStore struct {
}
// Delete provides a mock function with given fields: userId, termsOfServiceId
func (_m *UserTermsOfServiceStore) Delete(userId string, termsOfServiceId string) store.StoreChannel {
func (_m *UserTermsOfServiceStore) Delete(userId string, termsOfServiceId string) *model.AppError {
ret := _m.Called(userId, termsOfServiceId)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok {
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, string) *model.AppError); ok {
r0 = rf(userId, termsOfServiceId)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.AppError)
}
}
@@ -30,33 +29,51 @@ func (_m *UserTermsOfServiceStore) Delete(userId string, termsOfServiceId string
}
// GetByUser provides a mock function with given fields: userId
func (_m *UserTermsOfServiceStore) GetByUser(userId string) store.StoreChannel {
func (_m *UserTermsOfServiceStore) GetByUser(userId string) (*model.UserTermsOfService, *model.AppError) {
ret := _m.Called(userId)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok {
var r0 *model.UserTermsOfService
if rf, ok := ret.Get(0).(func(string) *model.UserTermsOfService); ok {
r0 = rf(userId)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.UserTermsOfService)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(userId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// Save provides a mock function with given fields: userTermsOfService
func (_m *UserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) store.StoreChannel {
func (_m *UserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError) {
ret := _m.Called(userTermsOfService)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(*model.UserTermsOfService) store.StoreChannel); ok {
var r0 *model.UserTermsOfService
if rf, ok := ret.Get(0).(func(*model.UserTermsOfService) *model.UserTermsOfService); ok {
r0 = rf(userTermsOfService)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.UserTermsOfService)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.UserTermsOfService) *model.AppError); ok {
r1 = rf(userTermsOfService)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTermsOfServiceStore(t *testing.T, ss store.Store) {
@@ -25,13 +26,9 @@ func testSaveTermsOfService(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u1))
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
r1 := <-ss.TermsOfService().Save(termsOfService)
savedTermsOfService, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
if r1.Err != nil {
t.Fatal(r1.Err)
}
savedTermsOfService := r1.Data.(*model.TermsOfService)
if len(savedTermsOfService.Id) != 26 {
t.Fatal("Id should have been populated")
}
@@ -49,14 +46,10 @@ func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u1))
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
store.Must(ss.TermsOfService().Save(termsOfService))
_, err := ss.TermsOfService().Save(termsOfService)
r1 := <-ss.TermsOfService().GetLatest(true)
if r1.Err != nil {
t.Fatal(r1.Err)
}
fetchedTermsOfService := r1.Data.(*model.TermsOfService)
fetchedTermsOfService, err := ss.TermsOfService().GetLatest(true)
require.Nil(t, err)
assert.Equal(t, termsOfService.Text, fetchedTermsOfService.Text)
assert.Equal(t, termsOfService.UserId, fetchedTermsOfService.UserId)
}
@@ -69,15 +62,14 @@ func testGetTermsOfService(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u1))
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
store.Must(ss.TermsOfService().Save(termsOfService))
_, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
r1 := <-ss.TermsOfService().Get("an_invalid_id", true)
assert.NotNil(t, r1.Err)
assert.Nil(t, r1.Data)
r1, err := ss.TermsOfService().Get("an_invalid_id", true)
assert.NotNil(t, err)
assert.Nil(t, r1)
r1 = <-ss.TermsOfService().Get(termsOfService.Id, true)
assert.Nil(t, r1.Err)
receivedTermsOfService := r1.Data.(*model.TermsOfService)
receivedTermsOfService, err := ss.TermsOfService().Get(termsOfService.Id, true)
assert.Nil(t, err)
assert.Equal(t, "terms of service", receivedTermsOfService.Text)
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUserTermsOfServiceStore(t *testing.T, ss store.Store) {
@@ -23,12 +24,8 @@ func testSaveUserTermsOfService(t *testing.T, ss store.Store) {
TermsOfServiceId: model.NewId(),
}
r1 := <-ss.UserTermsOfService().Save(userTermsOfService)
if r1.Err != nil {
t.Fatal(r1.Err)
}
savedUserTermsOfService := r1.Data.(*model.UserTermsOfService)
savedUserTermsOfService, err := ss.UserTermsOfService().Save(userTermsOfService)
require.Nil(t, err)
assert.Equal(t, userTermsOfService.UserId, savedUserTermsOfService.UserId)
assert.Equal(t, userTermsOfService.TermsOfServiceId, savedUserTermsOfService.TermsOfServiceId)
assert.NotEmpty(t, savedUserTermsOfService.CreateAt)
@@ -40,17 +37,11 @@ func testGetByUserTermsOfService(t *testing.T, ss store.Store) {
TermsOfServiceId: model.NewId(),
}
r1 := <-ss.UserTermsOfService().Save(userTermsOfService)
if r1.Err != nil {
t.Fatal(r1.Err)
}
_, err := ss.UserTermsOfService().Save(userTermsOfService)
require.Nil(t, err)
r1 = <-ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
if r1.Err != nil {
t.Fatal(r1.Err)
}
fetchedUserTermsOfService := r1.Data.(*model.UserTermsOfService)
fetchedUserTermsOfService, err := ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
require.Nil(t, err)
assert.Equal(t, userTermsOfService.UserId, fetchedUserTermsOfService.UserId)
assert.Equal(t, userTermsOfService.TermsOfServiceId, fetchedUserTermsOfService.TermsOfServiceId)
assert.NotEmpty(t, fetchedUserTermsOfService.CreateAt)
@@ -62,21 +53,15 @@ func testDeleteUserTermsOfService(t *testing.T, ss store.Store) {
TermsOfServiceId: model.NewId(),
}
r1 := <-ss.UserTermsOfService().Save(userTermsOfService)
if r1.Err != nil {
t.Fatal(r1.Err)
}
_, err := ss.UserTermsOfService().Save(userTermsOfService)
require.Nil(t, err)
r1 = <-ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
if r1.Err != nil {
t.Fatal(r1.Err)
}
_, err = ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
require.Nil(t, err)
r1 = <-ss.UserTermsOfService().Delete(userTermsOfService.UserId, userTermsOfService.TermsOfServiceId)
if r1.Err != nil {
t.Fatal(r1.Err)
}
err = ss.UserTermsOfService().Delete(userTermsOfService.UserId, userTermsOfService.TermsOfServiceId)
require.Nil(t, err)
r1 = <-ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
assert.Equal(t, "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", r1.Err.Id)
_, err = ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
assert.Equal(t, "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", err.Id)
}