github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/terms_of_service_store.go (about)

     1  // Copyright (c) 2017-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/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestTermsOfServiceStore(t *testing.T, ss store.Store) {
    15  	t.Run("TestSaveTermsOfService", func(t *testing.T) { testSaveTermsOfService(t, ss) })
    16  	t.Run("TestGetLatestTermsOfService", func(t *testing.T) { testGetLatestTermsOfService(t, ss) })
    17  	t.Run("TestGetTermsOfService", func(t *testing.T) { testGetTermsOfService(t, ss) })
    18  }
    19  
    20  func testSaveTermsOfService(t *testing.T, ss store.Store) {
    21  	u1 := model.User{}
    22  	u1.Username = model.NewId()
    23  	u1.Email = MakeEmail()
    24  	u1.Nickname = model.NewId()
    25  	store.Must(ss.User().Save(&u1))
    26  
    27  	termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
    28  	r1 := <-ss.TermsOfService().Save(termsOfService)
    29  
    30  	if r1.Err != nil {
    31  		t.Fatal(r1.Err)
    32  	}
    33  
    34  	savedTermsOfService := r1.Data.(*model.TermsOfService)
    35  	if len(savedTermsOfService.Id) != 26 {
    36  		t.Fatal("Id should have been populated")
    37  	}
    38  
    39  	if savedTermsOfService.CreateAt == 0 {
    40  		t.Fatal("Create at should have been populated")
    41  	}
    42  }
    43  
    44  func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
    45  	u1 := model.User{}
    46  	u1.Username = model.NewId()
    47  	u1.Email = MakeEmail()
    48  	u1.Nickname = model.NewId()
    49  	store.Must(ss.User().Save(&u1))
    50  
    51  	termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
    52  	store.Must(ss.TermsOfService().Save(termsOfService))
    53  
    54  	r1 := <-ss.TermsOfService().GetLatest(true)
    55  	if r1.Err != nil {
    56  		t.Fatal(r1.Err)
    57  	}
    58  
    59  	fetchedTermsOfService := r1.Data.(*model.TermsOfService)
    60  	assert.Equal(t, termsOfService.Text, fetchedTermsOfService.Text)
    61  	assert.Equal(t, termsOfService.UserId, fetchedTermsOfService.UserId)
    62  }
    63  
    64  func testGetTermsOfService(t *testing.T, ss store.Store) {
    65  	u1 := model.User{}
    66  	u1.Username = model.NewId()
    67  	u1.Email = MakeEmail()
    68  	u1.Nickname = model.NewId()
    69  	store.Must(ss.User().Save(&u1))
    70  
    71  	termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
    72  	store.Must(ss.TermsOfService().Save(termsOfService))
    73  
    74  	r1 := <-ss.TermsOfService().Get("an_invalid_id", true)
    75  	assert.NotNil(t, r1.Err)
    76  	assert.Nil(t, r1.Data)
    77  
    78  	r1 = <-ss.TermsOfService().Get(termsOfService.Id, true)
    79  	assert.Nil(t, r1.Err)
    80  
    81  	receivedTermsOfService := r1.Data.(*model.TermsOfService)
    82  	assert.Equal(t, "terms of service", receivedTermsOfService.Text)
    83  }