github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+incompatible/api4/status_test.go (about)

     1  package api4
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mattermost/mattermost-server/model"
     7  )
     8  
     9  func TestGetUserStatus(t *testing.T) {
    10  	th := Setup().InitBasic()
    11  	defer th.TearDown()
    12  	Client := th.Client
    13  
    14  	userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
    15  	CheckNoError(t, resp)
    16  	if userStatus.Status != "offline" {
    17  		t.Fatal("Should return offline status")
    18  	}
    19  
    20  	th.App.SetStatusOnline(th.BasicUser.Id, true)
    21  	userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
    22  	CheckNoError(t, resp)
    23  	if userStatus.Status != "online" {
    24  		t.Fatal("Should return online status")
    25  	}
    26  
    27  	th.App.SetStatusAwayIfNeeded(th.BasicUser.Id, true)
    28  	userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
    29  	CheckNoError(t, resp)
    30  	if userStatus.Status != "away" {
    31  		t.Fatal("Should return away status")
    32  	}
    33  
    34  	th.App.SetStatusDoNotDisturb(th.BasicUser.Id)
    35  	userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
    36  	CheckNoError(t, resp)
    37  	if userStatus.Status != "dnd" {
    38  		t.Fatal("Should return dnd status")
    39  	}
    40  
    41  	th.App.SetStatusOffline(th.BasicUser.Id, true)
    42  	userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
    43  	CheckNoError(t, resp)
    44  	if userStatus.Status != "offline" {
    45  		t.Fatal("Should return offline status")
    46  	}
    47  
    48  	//Get user2 status logged as user1
    49  	userStatus, resp = Client.GetUserStatus(th.BasicUser2.Id, "")
    50  	CheckNoError(t, resp)
    51  	if userStatus.Status != "offline" {
    52  		t.Fatal("Should return offline status")
    53  	}
    54  
    55  	Client.Logout()
    56  
    57  	_, resp = Client.GetUserStatus(th.BasicUser2.Id, "")
    58  	CheckUnauthorizedStatus(t, resp)
    59  
    60  	th.LoginBasic2()
    61  	userStatus, resp = Client.GetUserStatus(th.BasicUser2.Id, "")
    62  	CheckNoError(t, resp)
    63  	if userStatus.Status != "offline" {
    64  		t.Fatal("Should return offline status")
    65  	}
    66  }
    67  
    68  func TestGetUsersStatusesByIds(t *testing.T) {
    69  	th := Setup().InitBasic()
    70  	defer th.TearDown()
    71  	Client := th.Client
    72  
    73  	usersIds := []string{th.BasicUser.Id, th.BasicUser2.Id}
    74  
    75  	usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
    76  	CheckNoError(t, resp)
    77  	for _, userStatus := range usersStatuses {
    78  		if userStatus.Status != "offline" {
    79  			t.Fatal("Status should be offline")
    80  		}
    81  	}
    82  
    83  	th.App.SetStatusOnline(th.BasicUser.Id, true)
    84  	th.App.SetStatusOnline(th.BasicUser2.Id, true)
    85  	usersStatuses, resp = Client.GetUsersStatusesByIds(usersIds)
    86  	CheckNoError(t, resp)
    87  	for _, userStatus := range usersStatuses {
    88  		if userStatus.Status != "online" {
    89  			t.Fatal("Status should be offline")
    90  		}
    91  	}
    92  
    93  	th.App.SetStatusAwayIfNeeded(th.BasicUser.Id, true)
    94  	th.App.SetStatusAwayIfNeeded(th.BasicUser2.Id, true)
    95  	usersStatuses, resp = Client.GetUsersStatusesByIds(usersIds)
    96  	CheckNoError(t, resp)
    97  	for _, userStatus := range usersStatuses {
    98  		if userStatus.Status != "away" {
    99  			t.Fatal("Status should be offline")
   100  		}
   101  	}
   102  
   103  	th.App.SetStatusDoNotDisturb(th.BasicUser.Id)
   104  	th.App.SetStatusDoNotDisturb(th.BasicUser2.Id)
   105  	usersStatuses, resp = Client.GetUsersStatusesByIds(usersIds)
   106  	CheckNoError(t, resp)
   107  	for _, userStatus := range usersStatuses {
   108  		if userStatus.Status != "dnd" {
   109  			t.Fatal("Status should be offline")
   110  		}
   111  	}
   112  
   113  	Client.Logout()
   114  
   115  	_, resp = Client.GetUsersStatusesByIds(usersIds)
   116  	CheckUnauthorizedStatus(t, resp)
   117  }
   118  
   119  func TestUpdateUserStatus(t *testing.T) {
   120  	th := Setup().InitBasic().InitSystemAdmin()
   121  	defer th.TearDown()
   122  	Client := th.Client
   123  
   124  	toUpdateUserStatus := &model.Status{Status: "online", UserId: th.BasicUser.Id}
   125  	updateUserStatus, resp := Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
   126  	CheckNoError(t, resp)
   127  	if updateUserStatus.Status != "online" {
   128  		t.Fatal("Should return online status")
   129  	}
   130  
   131  	toUpdateUserStatus.Status = "away"
   132  	updateUserStatus, resp = Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
   133  	CheckNoError(t, resp)
   134  	if updateUserStatus.Status != "away" {
   135  		t.Fatal("Should return away status")
   136  	}
   137  
   138  	toUpdateUserStatus.Status = "dnd"
   139  	updateUserStatus, resp = Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
   140  	CheckNoError(t, resp)
   141  	if updateUserStatus.Status != "dnd" {
   142  		t.Fatal("Should return dnd status")
   143  	}
   144  
   145  	toUpdateUserStatus.Status = "offline"
   146  	updateUserStatus, resp = Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
   147  	CheckNoError(t, resp)
   148  	if updateUserStatus.Status != "offline" {
   149  		t.Fatal("Should return offline status")
   150  	}
   151  
   152  	toUpdateUserStatus.Status = "online"
   153  	toUpdateUserStatus.UserId = th.BasicUser2.Id
   154  	_, resp = Client.UpdateUserStatus(th.BasicUser2.Id, toUpdateUserStatus)
   155  	CheckForbiddenStatus(t, resp)
   156  
   157  	toUpdateUserStatus.Status = "online"
   158  	updateUserStatus, _ = th.SystemAdminClient.UpdateUserStatus(th.BasicUser2.Id, toUpdateUserStatus)
   159  	if updateUserStatus.Status != "online" {
   160  		t.Fatal("Should return online status")
   161  	}
   162  
   163  	_, resp = Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
   164  	CheckBadRequestStatus(t, resp)
   165  
   166  	Client.Logout()
   167  
   168  	_, resp = Client.UpdateUserStatus(th.BasicUser2.Id, toUpdateUserStatus)
   169  	CheckUnauthorizedStatus(t, resp)
   170  }