github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/user_access_token_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestUserAccessTokenJson(t *testing.T) {
    12  	a1 := UserAccessToken{}
    13  	a1.UserId = NewId()
    14  	a1.Token = NewId()
    15  
    16  	json := a1.ToJson()
    17  	ra1 := UserAccessTokenFromJson(strings.NewReader(json))
    18  
    19  	if a1.Token != ra1.Token {
    20  		t.Fatal("tokens didn't match")
    21  	}
    22  
    23  	tokens := []*UserAccessToken{&a1}
    24  	json = UserAccessTokenListToJson(tokens)
    25  	tokens = UserAccessTokenListFromJson(strings.NewReader(json))
    26  
    27  	if tokens[0].Token != a1.Token {
    28  		t.Fatal("tokens didn't match")
    29  	}
    30  }
    31  
    32  func TestUserAccessTokenIsValid(t *testing.T) {
    33  	ad := UserAccessToken{}
    34  
    35  	if err := ad.IsValid(); err == nil || err.Id != "model.user_access_token.is_valid.id.app_error" {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	ad.Id = NewRandomString(26)
    40  	if err := ad.IsValid(); err == nil || err.Id != "model.user_access_token.is_valid.token.app_error" {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	ad.Token = NewRandomString(26)
    45  	if err := ad.IsValid(); err == nil || err.Id != "model.user_access_token.is_valid.user_id.app_error" {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	ad.UserId = NewRandomString(26)
    50  	if err := ad.IsValid(); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	ad.Description = NewRandomString(256)
    55  	if err := ad.IsValid(); err == nil || err.Id != "model.user_access_token.is_valid.description.app_error" {
    56  		t.Fatal(err)
    57  	}
    58  }