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

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"sort"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/store"
    15  )
    16  
    17  func TestStatusStore(t *testing.T, ss store.Store) {
    18  	t.Run("", func(t *testing.T) { testStatusStore(t, ss) })
    19  	t.Run("ActiveUserCount", func(t *testing.T) { testActiveUserCount(t, ss) })
    20  	t.Run("GetAllFromTeam", func(t *testing.T) { testGetAllFromTeam(t, ss) })
    21  }
    22  
    23  func testStatusStore(t *testing.T, ss store.Store) {
    24  	status := &model.Status{UserId: model.NewId(), Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
    25  
    26  	if err := (<-ss.Status().SaveOrUpdate(status)).Err; err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	status.LastActivityAt = 10
    31  
    32  	if err := (<-ss.Status().SaveOrUpdate(status)).Err; err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	if err := (<-ss.Status().Get(status.UserId)).Err; err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	status2 := &model.Status{UserId: model.NewId(), Status: model.STATUS_AWAY, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
    41  	if err := (<-ss.Status().SaveOrUpdate(status2)).Err; err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	status3 := &model.Status{UserId: model.NewId(), Status: model.STATUS_OFFLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
    46  	if err := (<-ss.Status().SaveOrUpdate(status3)).Err; err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	if result := <-ss.Status().GetOnlineAway(); result.Err != nil {
    51  		t.Fatal(result.Err)
    52  	} else {
    53  		statuses := result.Data.([]*model.Status)
    54  		for _, status := range statuses {
    55  			if status.Status == model.STATUS_OFFLINE {
    56  				t.Fatal("should not have returned offline statuses")
    57  			}
    58  		}
    59  	}
    60  
    61  	if result := <-ss.Status().GetOnline(); result.Err != nil {
    62  		t.Fatal(result.Err)
    63  	} else {
    64  		statuses := result.Data.([]*model.Status)
    65  		for _, status := range statuses {
    66  			if status.Status != model.STATUS_ONLINE {
    67  				t.Fatal("should not have returned offline statuses")
    68  			}
    69  		}
    70  	}
    71  
    72  	if result := <-ss.Status().GetByIds([]string{status.UserId, "junk"}); result.Err != nil {
    73  		t.Fatal(result.Err)
    74  	} else {
    75  		statuses := result.Data.([]*model.Status)
    76  		if len(statuses) != 1 {
    77  			t.Fatal("should only have 1 status")
    78  		}
    79  	}
    80  
    81  	if err := (<-ss.Status().ResetAll()).Err; err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	if result := <-ss.Status().Get(status.UserId); result.Err != nil {
    86  		t.Fatal(result.Err)
    87  	} else {
    88  		status := result.Data.(*model.Status)
    89  		if status.Status != model.STATUS_OFFLINE {
    90  			t.Fatal("should be offline")
    91  		}
    92  	}
    93  
    94  	if result := <-ss.Status().UpdateLastActivityAt(status.UserId, 10); result.Err != nil {
    95  		t.Fatal(result.Err)
    96  	}
    97  }
    98  
    99  func testActiveUserCount(t *testing.T, ss store.Store) {
   100  	status := &model.Status{UserId: model.NewId(), Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""}
   101  	store.Must(ss.Status().SaveOrUpdate(status))
   102  
   103  	if result := <-ss.Status().GetTotalActiveUsersCount(); result.Err != nil {
   104  		t.Fatal(result.Err)
   105  	} else {
   106  		count := result.Data.(int64)
   107  		require.True(t, count > 0, "expected count > 0, got %d", count)
   108  	}
   109  }
   110  
   111  type ByUserId []*model.Status
   112  
   113  func (s ByUserId) Len() int           { return len(s) }
   114  func (s ByUserId) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
   115  func (s ByUserId) Less(i, j int) bool { return s[i].UserId < s[j].UserId }
   116  
   117  func testGetAllFromTeam(t *testing.T, ss store.Store) {
   118  	assertStatuses := func(expected, actual []*model.Status) {
   119  		sort.Sort(ByUserId(expected))
   120  		sort.Sort(ByUserId(actual))
   121  		assert.Equal(t, expected, actual)
   122  	}
   123  
   124  	team1 := model.Team{}
   125  	team1.DisplayName = model.NewId()
   126  	team1.Name = model.NewId()
   127  	team1.Email = MakeEmail()
   128  	team1.Type = model.TEAM_OPEN
   129  
   130  	if err := (<-ss.Team().Save(&team1)).Err; err != nil {
   131  		t.Fatal("couldn't save team", err)
   132  	}
   133  
   134  	team2 := model.Team{}
   135  	team2.DisplayName = model.NewId()
   136  	team2.Name = model.NewId()
   137  	team2.Email = MakeEmail()
   138  	team2.Type = model.TEAM_OPEN
   139  
   140  	if err := (<-ss.Team().Save(&team2)).Err; err != nil {
   141  		t.Fatal("couldn't save team", err)
   142  	}
   143  
   144  	team1Member1 := &model.TeamMember{TeamId: team1.Id, UserId: model.NewId()}
   145  	if response := <-ss.Team().SaveMember(team1Member1, -1); response.Err != nil {
   146  		t.Fatal(response.Err)
   147  	}
   148  	team1Member2 := &model.TeamMember{TeamId: team1.Id, UserId: model.NewId()}
   149  	if response := <-ss.Team().SaveMember(team1Member2, -1); response.Err != nil {
   150  		t.Fatal(response.Err)
   151  	}
   152  	team2Member1 := &model.TeamMember{TeamId: team2.Id, UserId: model.NewId()}
   153  	if response := <-ss.Team().SaveMember(team2Member1, -1); response.Err != nil {
   154  		t.Fatal(response.Err)
   155  	}
   156  	team2Member2 := &model.TeamMember{TeamId: team2.Id, UserId: model.NewId()}
   157  	if response := <-ss.Team().SaveMember(team2Member2, -1); response.Err != nil {
   158  		t.Fatal(response.Err)
   159  	}
   160  
   161  	team1Member1Status := &model.Status{UserId: team1Member1.UserId, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
   162  	if err := (<-ss.Status().SaveOrUpdate(team1Member1Status)).Err; err != nil {
   163  		t.Fatal(err)
   164  	}
   165  	team1Member2Status := &model.Status{UserId: team1Member2.UserId, Status: model.STATUS_OFFLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""}
   166  	if err := (<-ss.Status().SaveOrUpdate(team1Member2Status)).Err; err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	team2Member1Status := &model.Status{UserId: team2Member1.UserId, Status: model.STATUS_ONLINE, Manual: true, LastActivityAt: model.GetMillis(), ActiveChannel: ""}
   170  	if err := (<-ss.Status().SaveOrUpdate(team2Member1Status)).Err; err != nil {
   171  		t.Fatal(err)
   172  	}
   173  	team2Member2Status := &model.Status{UserId: team2Member2.UserId, Status: model.STATUS_OFFLINE, Manual: true, LastActivityAt: model.GetMillis(), ActiveChannel: ""}
   174  	if err := (<-ss.Status().SaveOrUpdate(team2Member2Status)).Err; err != nil {
   175  		t.Fatal(err)
   176  	}
   177  
   178  	if result := <-ss.Status().GetAllFromTeam(team1.Id); result.Err != nil {
   179  		t.Fatal(result.Err)
   180  	} else {
   181  		assertStatuses([]*model.Status{
   182  			team1Member1Status,
   183  			team1Member2Status,
   184  		}, result.Data.([]*model.Status))
   185  	}
   186  
   187  	if result := <-ss.Status().GetAllFromTeam(team2.Id); result.Err != nil {
   188  		t.Fatal(result.Err)
   189  	} else {
   190  		assertStatuses([]*model.Status{
   191  			team2Member1Status,
   192  			team2Member2Status,
   193  		}, result.Data.([]*model.Status))
   194  	}
   195  }