github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+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  
    12  func TestStatus(t *testing.T) {
    13  	status := Status{NewId(), STATUS_ONLINE, true, 0, ""}
    14  	json := status.ToJson()
    15  	status2 := StatusFromJson(strings.NewReader(json))
    16  
    17  	if status.UserId != status2.UserId {
    18  		t.Fatal("UserId should have matched")
    19  	}
    20  
    21  	if status.Status != status2.Status {
    22  		t.Fatal("Status should have matched")
    23  	}
    24  
    25  	if status.LastActivityAt != status2.LastActivityAt {
    26  		t.Fatal("LastActivityAt should have matched")
    27  	}
    28  
    29  	if status.Manual != status2.Manual {
    30  		t.Fatal("Manual should have matched")
    31  	}
    32  }
    33  
    34  func TestStatusListToJson(t *testing.T) {
    35  	statuses := []*Status{{NewId(), STATUS_ONLINE, true, 0, ""}, {NewId(), STATUS_OFFLINE, true, 0, ""}}
    36  	jsonStatuses := StatusListToJson(statuses)
    37  
    38  	var dat []map[string]interface{}
    39  	if err := json.Unmarshal([]byte(jsonStatuses), &dat); err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	if len(dat) != 2 {
    44  		t.Fatal("Status array should contain 2 elements")
    45  	}
    46  	if statuses[0].UserId != dat[0]["user_id"] {
    47  		t.Fatal("UserId should be equal")
    48  	}
    49  	if statuses[1].UserId != dat[1]["user_id"] {
    50  		t.Fatal("UserId should be equal")
    51  	}
    52  }
    53  
    54  func TestStatusListFromJson(t *testing.T) {
    55  	const jsonStream = `
    56      		 [{"user_id":"k39fowpzhfffjxeaw8ecyrygme","status":"online","manual":true,"last_activity_at":0},{"user_id":"e9f1bbg8wfno7b3k7yk79bbwfy","status":"offline","manual":true,"last_activity_at":0}]
    57      	`
    58  	var dat []map[string]interface{}
    59  	if err := json.Unmarshal([]byte(jsonStream), &dat); err != nil {
    60  		panic(err)
    61  	}
    62  
    63  	toDec := strings.NewReader(jsonStream)
    64  	statusesFromJson := StatusListFromJson(toDec)
    65  
    66  	if statusesFromJson[0].UserId != dat[0]["user_id"] {
    67  		t.Fatal("UserId should be equal")
    68  	}
    69  	if statusesFromJson[1].UserId != dat[1]["user_id"] {
    70  		t.Fatal("UserId should be equal")
    71  	}
    72  }