github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/api/status_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/store"
    13  )
    14  
    15  func TestStatuses(t *testing.T) {
    16  	th := Setup().InitBasic()
    17  	defer th.TearDown()
    18  
    19  	Client := th.BasicClient
    20  	WebSocketClient, err := th.CreateWebSocketClient()
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer WebSocketClient.Close()
    25  	WebSocketClient.Listen()
    26  
    27  	time.Sleep(300 * time.Millisecond)
    28  	if resp := <-WebSocketClient.ResponseChannel; resp.Status != model.STATUS_OK {
    29  		t.Fatal("should have responded OK to authentication challenge")
    30  	}
    31  
    32  	team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
    33  	rteam, _ := Client.CreateTeam(&team)
    34  
    35  	user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
    36  	ruser := Client.Must(Client.CreateUser(&user, "")).Data.(*model.User)
    37  	th.LinkUserToTeam(ruser, rteam.Data.(*model.Team))
    38  	store.Must(th.App.Srv.Store.User().VerifyEmail(ruser.Id))
    39  
    40  	user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
    41  	ruser2 := Client.Must(Client.CreateUser(&user2, "")).Data.(*model.User)
    42  	th.LinkUserToTeam(ruser2, rteam.Data.(*model.Team))
    43  	store.Must(th.App.Srv.Store.User().VerifyEmail(ruser2.Id))
    44  
    45  	Client.Login(user.Email, user.Password)
    46  	Client.SetTeamId(team.Id)
    47  
    48  	r1, err := Client.GetStatuses()
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	statuses := r1.Data.(map[string]string)
    54  
    55  	for _, status := range statuses {
    56  		if status != model.STATUS_OFFLINE && status != model.STATUS_AWAY && status != model.STATUS_ONLINE {
    57  			t.Fatal("one of the statuses had an invalid value")
    58  		}
    59  	}
    60  
    61  	th.LoginBasic2()
    62  
    63  	WebSocketClient2, err2 := th.CreateWebSocketClient()
    64  	if err2 != nil {
    65  		t.Fatal(err2)
    66  	}
    67  
    68  	time.Sleep(1000 * time.Millisecond)
    69  
    70  	WebSocketClient.GetStatuses()
    71  	if resp := <-WebSocketClient.ResponseChannel; resp.Error != nil {
    72  		t.Fatal(resp.Error)
    73  	} else {
    74  		if resp.SeqReply != WebSocketClient.Sequence-1 {
    75  			t.Fatal("bad sequence number")
    76  		}
    77  
    78  		for _, status := range resp.Data {
    79  			if status != model.STATUS_OFFLINE && status != model.STATUS_AWAY && status != model.STATUS_ONLINE {
    80  				t.Fatal("one of the statuses had an invalid value")
    81  			}
    82  		}
    83  
    84  		if status, ok := resp.Data[th.BasicUser2.Id]; !ok {
    85  			t.Log(resp.Data)
    86  			t.Fatal("should have had user status")
    87  		} else if status != model.STATUS_ONLINE {
    88  			t.Log(status)
    89  			t.Fatal("status should have been online")
    90  		}
    91  	}
    92  
    93  	WebSocketClient.GetStatusesByIds([]string{th.BasicUser2.Id})
    94  	if resp := <-WebSocketClient.ResponseChannel; resp.Error != nil {
    95  		t.Fatal(resp.Error)
    96  	} else {
    97  		if resp.SeqReply != WebSocketClient.Sequence-1 {
    98  			t.Fatal("bad sequence number")
    99  		}
   100  
   101  		for _, status := range resp.Data {
   102  			if status != model.STATUS_OFFLINE && status != model.STATUS_AWAY && status != model.STATUS_ONLINE {
   103  				t.Fatal("one of the statuses had an invalid value")
   104  			}
   105  		}
   106  
   107  		if status, ok := resp.Data[th.BasicUser2.Id]; !ok {
   108  			t.Log(len(resp.Data))
   109  			t.Fatal("should have had user status")
   110  		} else if status != model.STATUS_ONLINE {
   111  			t.Log(status)
   112  			t.Fatal("status should have been online")
   113  		} else if len(resp.Data) != 1 {
   114  			t.Fatal("only 1 status should be returned")
   115  		}
   116  	}
   117  
   118  	WebSocketClient.GetStatusesByIds([]string{ruser2.Id, "junk"})
   119  	if resp := <-WebSocketClient.ResponseChannel; resp.Error != nil {
   120  		t.Fatal(resp.Error)
   121  	} else {
   122  		if resp.SeqReply != WebSocketClient.Sequence-1 {
   123  			t.Fatal("bad sequence number")
   124  		}
   125  
   126  		if len(resp.Data) != 2 {
   127  			t.Fatal("2 statuses should be returned")
   128  		}
   129  	}
   130  
   131  	WebSocketClient.GetStatusesByIds([]string{})
   132  	if resp := <-WebSocketClient.ResponseChannel; resp.Error == nil {
   133  		if resp.SeqReply != WebSocketClient.Sequence-1 {
   134  			t.Fatal("bad sequence number")
   135  		}
   136  		t.Fatal("should have errored - empty user ids")
   137  	}
   138  
   139  	WebSocketClient2.Close()
   140  
   141  	th.App.SetStatusAwayIfNeeded(th.BasicUser.Id, false)
   142  
   143  	awayTimeout := *th.App.Config().TeamSettings.UserStatusAwayTimeout
   144  	defer func() {
   145  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.UserStatusAwayTimeout = awayTimeout })
   146  	}()
   147  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.UserStatusAwayTimeout = 1 })
   148  
   149  	time.Sleep(1500 * time.Millisecond)
   150  
   151  	th.App.SetStatusAwayIfNeeded(th.BasicUser.Id, false)
   152  	th.App.SetStatusOnline(th.BasicUser.Id, "junk", false)
   153  
   154  	time.Sleep(1500 * time.Millisecond)
   155  
   156  	WebSocketClient.GetStatuses()
   157  	if resp := <-WebSocketClient.ResponseChannel; resp.Error != nil {
   158  		t.Fatal(resp.Error)
   159  	} else {
   160  		if resp.SeqReply != WebSocketClient.Sequence-1 {
   161  			t.Fatal("bad sequence number")
   162  		}
   163  
   164  		if _, ok := resp.Data[th.BasicUser2.Id]; ok {
   165  			t.Fatal("should not have had user status")
   166  		}
   167  	}
   168  
   169  	stop := make(chan bool)
   170  	onlineHit := false
   171  	awayHit := false
   172  
   173  	go func() {
   174  		for {
   175  			select {
   176  			case resp := <-WebSocketClient.EventChannel:
   177  				if resp.Event == model.WEBSOCKET_EVENT_STATUS_CHANGE && resp.Data["user_id"].(string) == th.BasicUser.Id {
   178  					status := resp.Data["status"].(string)
   179  					if status == model.STATUS_ONLINE {
   180  						onlineHit = true
   181  					} else if status == model.STATUS_AWAY {
   182  						awayHit = true
   183  					}
   184  				}
   185  			case <-stop:
   186  				return
   187  			}
   188  		}
   189  	}()
   190  
   191  	time.Sleep(500 * time.Millisecond)
   192  
   193  	stop <- true
   194  
   195  	if !onlineHit {
   196  		t.Fatal("didn't get online event")
   197  	}
   198  	if !awayHit {
   199  		t.Fatal("didn't get away event")
   200  	}
   201  
   202  	time.Sleep(500 * time.Millisecond)
   203  
   204  	WebSocketClient.Close()
   205  }
   206  
   207  func TestGetStatusesByIds(t *testing.T) {
   208  	th := Setup().InitBasic()
   209  	defer th.TearDown()
   210  
   211  	Client := th.BasicClient
   212  
   213  	if result, err := Client.GetStatusesByIds([]string{th.BasicUser.Id}); err != nil {
   214  		t.Fatal(err)
   215  	} else {
   216  		statuses := result.Data.(map[string]string)
   217  		if len(statuses) != 1 {
   218  			t.Fatal("should only have 1 status")
   219  		}
   220  	}
   221  
   222  	if result, err := Client.GetStatusesByIds([]string{th.BasicUser.Id, th.BasicUser2.Id, "junk"}); err != nil {
   223  		t.Fatal(err)
   224  	} else {
   225  		statuses := result.Data.(map[string]string)
   226  		if len(statuses) != 3 {
   227  			t.Fatal("should have 3 statuses")
   228  		}
   229  	}
   230  
   231  	if _, err := Client.GetStatusesByIds([]string{}); err == nil {
   232  		t.Fatal("should have errored")
   233  	}
   234  }