github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/terms_of_service_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/model"
    13  	"github.com/mattermost/mattermost-server/v5/store"
    14  )
    15  
    16  func TestTermsOfServiceStore(t *testing.T, ss store.Store) {
    17  	t.Run("TestSaveTermsOfService", func(t *testing.T) { testSaveTermsOfService(t, ss) })
    18  	t.Run("TestGetLatestTermsOfService", func(t *testing.T) { testGetLatestTermsOfService(t, ss) })
    19  	t.Run("TestGetTermsOfService", func(t *testing.T) { testGetTermsOfService(t, ss) })
    20  }
    21  
    22  func cleanUpTOS(ss store.Store) {
    23  	// Clearing out the table before starting the test.
    24  	// Otherwise the row inserted by the previous Save call from testSaveTermsOfService
    25  	// gets picked up.
    26  	// We call DropAllTables but we actually need to delete only TermsOfService.
    27  	// However, there is no straightforward way to just clear that table without introducing
    28  	// new methods. So we use the hammer.
    29  	ss.DropAllTables()
    30  }
    31  
    32  func testSaveTermsOfService(t *testing.T, ss store.Store) {
    33  	t.Cleanup(func() { cleanUpTOS(ss) })
    34  
    35  	u1 := model.User{}
    36  	u1.Username = model.NewId()
    37  	u1.Email = MakeEmail()
    38  	u1.Nickname = model.NewId()
    39  	_, err := ss.User().Save(&u1)
    40  	require.NoError(t, err)
    41  
    42  	termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
    43  	savedTermsOfService, err := ss.TermsOfService().Save(termsOfService)
    44  	require.NoError(t, err)
    45  
    46  	require.Len(t, savedTermsOfService.Id, 26, "Id should have been populated")
    47  
    48  	require.NotEqual(t, savedTermsOfService.CreateAt, 0, "Create at should have been populated")
    49  }
    50  
    51  func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
    52  	t.Cleanup(func() { cleanUpTOS(ss) })
    53  
    54  	u1 := model.User{}
    55  	u1.Username = model.NewId()
    56  	u1.Email = MakeEmail()
    57  	u1.Nickname = model.NewId()
    58  	_, err := ss.User().Save(&u1)
    59  	require.NoError(t, err)
    60  
    61  	termsOfService := &model.TermsOfService{Text: "terms of service 2", UserId: u1.Id}
    62  	_, err = ss.TermsOfService().Save(termsOfService)
    63  	require.NoError(t, err)
    64  
    65  	fetchedTermsOfService, err := ss.TermsOfService().GetLatest(true)
    66  	require.NoError(t, err)
    67  	assert.Equal(t, termsOfService.Text, fetchedTermsOfService.Text)
    68  	assert.Equal(t, termsOfService.UserId, fetchedTermsOfService.UserId)
    69  }
    70  
    71  func testGetTermsOfService(t *testing.T, ss store.Store) {
    72  	t.Cleanup(func() { cleanUpTOS(ss) })
    73  
    74  	u1 := model.User{}
    75  	u1.Username = model.NewId()
    76  	u1.Email = MakeEmail()
    77  	u1.Nickname = model.NewId()
    78  	_, err := ss.User().Save(&u1)
    79  	require.NoError(t, err)
    80  
    81  	termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
    82  	_, err = ss.TermsOfService().Save(termsOfService)
    83  	require.NoError(t, err)
    84  
    85  	r1, err := ss.TermsOfService().Get("an_invalid_id", true)
    86  	assert.Error(t, err)
    87  	assert.Nil(t, r1)
    88  
    89  	receivedTermsOfService, err := ss.TermsOfService().Get(termsOfService.Id, true)
    90  	assert.NoError(t, err)
    91  	assert.Equal(t, "terms of service", receivedTermsOfService.Text)
    92  }