github.com/hahmadia/mattermost-server@v5.11.1+incompatible/model/status_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestStatus(t *testing.T) {
    15  	status := Status{NewId(), STATUS_ONLINE, true, 0, "123"}
    16  	json := status.ToJson()
    17  	status2 := StatusFromJson(strings.NewReader(json))
    18  
    19  	if status.UserId != status2.UserId {
    20  		t.Fatal("UserId should have matched")
    21  	}
    22  
    23  	if status.Status != status2.Status {
    24  		t.Fatal("Status should have matched")
    25  	}
    26  
    27  	if status.LastActivityAt != status2.LastActivityAt {
    28  		t.Fatal("LastActivityAt should have matched")
    29  	}
    30  
    31  	if status.Manual != status2.Manual {
    32  		t.Fatal("Manual should have matched")
    33  	}
    34  
    35  	assert.Equal(t, "", status2.ActiveChannel)
    36  
    37  	json = status.ToClusterJson()
    38  	status2 = StatusFromJson(strings.NewReader(json))
    39  
    40  	assert.Equal(t, status.ActiveChannel, status2.ActiveChannel)
    41  }
    42  
    43  func TestStatusListToJson(t *testing.T) {
    44  	statuses := []*Status{{NewId(), STATUS_ONLINE, true, 0, "123"}, {NewId(), STATUS_OFFLINE, true, 0, ""}}
    45  	jsonStatuses := StatusListToJson(statuses)
    46  
    47  	var dat []map[string]interface{}
    48  	if err := json.Unmarshal([]byte(jsonStatuses), &dat); err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	assert.Equal(t, len(dat), 2)
    53  
    54  	_, ok := dat[0]["active_channel"]
    55  	assert.False(t, ok)
    56  	assert.Equal(t, statuses[0].ActiveChannel, "123")
    57  	assert.Equal(t, statuses[0].UserId, dat[0]["user_id"])
    58  	assert.Equal(t, statuses[1].UserId, dat[1]["user_id"])
    59  }
    60  
    61  func TestStatusListFromJson(t *testing.T) {
    62  	const jsonStream = `
    63      		 [{"user_id":"k39fowpzhfffjxeaw8ecyrygme","status":"online","manual":true,"last_activity_at":0},{"user_id":"e9f1bbg8wfno7b3k7yk79bbwfy","status":"offline","manual":true,"last_activity_at":0}]
    64      	`
    65  	var dat []map[string]interface{}
    66  	if err := json.Unmarshal([]byte(jsonStream), &dat); err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	toDec := strings.NewReader(jsonStream)
    71  	statusesFromJson := StatusListFromJson(toDec)
    72  
    73  	if statusesFromJson[0].UserId != dat[0]["user_id"] {
    74  		t.Fatal("UserId should be equal")
    75  	}
    76  	if statusesFromJson[1].UserId != dat[1]["user_id"] {
    77  		t.Fatal("UserId should be equal")
    78  	}
    79  }