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