github.com/cryptomisa/mattermost-server@v5.11.1+incompatible/model/authorize_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 TestAuthJson(t *testing.T) {
    14  	a1 := AuthData{}
    15  	a1.ClientId = NewId()
    16  	a1.UserId = NewId()
    17  	a1.Code = NewId()
    18  
    19  	json := a1.ToJson()
    20  	ra1 := AuthDataFromJson(strings.NewReader(json))
    21  
    22  	if a1.Code != ra1.Code {
    23  		t.Fatal("codes didn't match")
    24  	}
    25  
    26  	a2 := AuthorizeRequest{}
    27  	a2.ClientId = NewId()
    28  	a2.Scope = NewId()
    29  
    30  	json = a2.ToJson()
    31  	ra2 := AuthorizeRequestFromJson(strings.NewReader(json))
    32  
    33  	if a2.ClientId != ra2.ClientId {
    34  		t.Fatal("client ids didn't match")
    35  	}
    36  }
    37  
    38  func TestAuthPreSave(t *testing.T) {
    39  	a1 := AuthData{}
    40  	a1.ClientId = NewId()
    41  	a1.UserId = NewId()
    42  	a1.Code = NewId()
    43  	a1.PreSave()
    44  	a1.IsExpired()
    45  }
    46  
    47  func TestAuthIsValid(t *testing.T) {
    48  
    49  	ad := AuthData{}
    50  
    51  	require.NotNil(t, ad.IsValid())
    52  
    53  	ad.ClientId = NewRandomString(28)
    54  	if err := ad.IsValid(); err == nil {
    55  		t.Fatal("Should have failed Client Id")
    56  	}
    57  
    58  	ad.ClientId = NewId()
    59  	require.NotNil(t, ad.IsValid())
    60  
    61  	ad.UserId = NewRandomString(28)
    62  	if err := ad.IsValid(); err == nil {
    63  		t.Fatal("Should have failed User Id")
    64  	}
    65  
    66  	ad.UserId = NewId()
    67  	require.NotNil(t, ad.IsValid())
    68  
    69  	ad.Code = NewRandomString(129)
    70  	if err := ad.IsValid(); err == nil {
    71  		t.Fatal("Should have failed Code to long")
    72  	}
    73  
    74  	ad.Code = ""
    75  	if err := ad.IsValid(); err == nil {
    76  		t.Fatal("Should have failed Code not set")
    77  	}
    78  
    79  	ad.Code = NewId()
    80  	require.NotNil(t, ad.IsValid())
    81  
    82  	ad.ExpiresIn = 0
    83  	if err := ad.IsValid(); err == nil {
    84  		t.Fatal("Should have failed invalid ExpiresIn")
    85  	}
    86  
    87  	ad.ExpiresIn = 1
    88  	require.NotNil(t, ad.IsValid())
    89  
    90  	ad.CreateAt = 0
    91  	if err := ad.IsValid(); err == nil {
    92  		t.Fatal("Should have failed Invalid Create At")
    93  	}
    94  
    95  	ad.CreateAt = 1
    96  	require.NotNil(t, ad.IsValid())
    97  
    98  	ad.State = NewRandomString(129)
    99  	if err := ad.IsValid(); err == nil {
   100  		t.Fatal("Should have failed invalid State")
   101  	}
   102  
   103  	ad.State = NewRandomString(128)
   104  	if err := ad.IsValid(); err == nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	ad.Scope = NewRandomString(1025)
   109  	if err := ad.IsValid(); err == nil {
   110  		t.Fatal("Should have failed invalid Scope")
   111  	}
   112  
   113  	ad.Scope = NewRandomString(128)
   114  	require.NotNil(t, ad.IsValid())
   115  
   116  	ad.RedirectUri = ""
   117  	if err := ad.IsValid(); err == nil {
   118  		t.Fatal("Should have failed Redirect URI not set")
   119  	}
   120  
   121  	ad.RedirectUri = NewRandomString(28)
   122  	if err := ad.IsValid(); err == nil {
   123  		t.Fatal("Should have failed invalid URL")
   124  	}
   125  
   126  	ad.RedirectUri = NewRandomString(257)
   127  	if err := ad.IsValid(); err == nil {
   128  		t.Fatal("Should have failed invalid URL")
   129  	}
   130  
   131  	ad.RedirectUri = "http://example.com"
   132  	if err := ad.IsValid(); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  }