github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/access_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  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestAccessJson(t *testing.T) {
    14  	a1 := AccessData{}
    15  	a1.ClientId = NewId()
    16  	a1.UserId = NewId()
    17  	a1.Token = NewId()
    18  	a1.RefreshToken = NewId()
    19  
    20  	json := a1.ToJson()
    21  	ra1 := AccessDataFromJson(strings.NewReader(json))
    22  
    23  	if a1.Token != ra1.Token {
    24  		t.Fatal("tokens didn't match")
    25  	}
    26  }
    27  
    28  func TestAccessIsValid(t *testing.T) {
    29  	ad := AccessData{}
    30  
    31  	require.NotNil(t, ad.IsValid())
    32  
    33  	ad.ClientId = NewRandomString(28)
    34  	if err := ad.IsValid(); err == nil {
    35  		t.Fatal("Should have failed Client Id")
    36  	}
    37  
    38  	ad.ClientId = ""
    39  	if err := ad.IsValid(); err == nil {
    40  		t.Fatal("Should have failed Client Id")
    41  	}
    42  
    43  	ad.ClientId = NewId()
    44  	require.NotNil(t, ad.IsValid())
    45  
    46  	ad.UserId = NewRandomString(28)
    47  	if err := ad.IsValid(); err == nil {
    48  		t.Fatal("Should have failed User Id")
    49  	}
    50  
    51  	ad.UserId = ""
    52  	if err := ad.IsValid(); err == nil {
    53  		t.Fatal("Should have failed User Id")
    54  	}
    55  
    56  	ad.UserId = NewId()
    57  	if err := ad.IsValid(); err == nil {
    58  		t.Fatal("should have failed")
    59  	}
    60  
    61  	ad.Token = NewRandomString(22)
    62  	if err := ad.IsValid(); err == nil {
    63  		t.Fatal("Should have failed Token")
    64  	}
    65  
    66  	ad.Token = NewId()
    67  	require.NotNil(t, ad.IsValid())
    68  
    69  	ad.RefreshToken = NewRandomString(28)
    70  	if err := ad.IsValid(); err == nil {
    71  		t.Fatal("Should have failed Refresh Token")
    72  	}
    73  
    74  	ad.RefreshToken = NewId()
    75  	require.NotNil(t, ad.IsValid())
    76  
    77  	ad.RedirectUri = ""
    78  	if err := ad.IsValid(); err == nil {
    79  		t.Fatal("Should have failed Redirect URI not set")
    80  	}
    81  
    82  	ad.RedirectUri = NewRandomString(28)
    83  	if err := ad.IsValid(); err == nil {
    84  		t.Fatal("Should have failed invalid URL")
    85  	}
    86  
    87  	ad.RedirectUri = "http://example.com"
    88  	if err := ad.IsValid(); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  }