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