PageRenderTime 498ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/model/authorize_test.go

https://gitlab.com/unofficial-mirrors/mattermost-platform
Go | 147 lines | 114 code | 31 blank | 2 comment | 44 complexity | 08985eb7ee879045c66fec219d76d6f8 MD5 | raw file
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See License.txt for license information.
  3. package model
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. func TestAuthJson(t *testing.T) {
  9. a1 := AuthData{}
  10. a1.ClientId = NewId()
  11. a1.UserId = NewId()
  12. a1.Code = NewId()
  13. json := a1.ToJson()
  14. ra1 := AuthDataFromJson(strings.NewReader(json))
  15. if a1.Code != ra1.Code {
  16. t.Fatal("codes didn't match")
  17. }
  18. a2 := AuthorizeRequest{}
  19. a2.ClientId = NewId()
  20. a2.Scope = NewId()
  21. json = a2.ToJson()
  22. ra2 := AuthorizeRequestFromJson(strings.NewReader(json))
  23. if a2.ClientId != ra2.ClientId {
  24. t.Fatal("client ids didn't match")
  25. }
  26. }
  27. func TestAuthPreSave(t *testing.T) {
  28. a1 := AuthData{}
  29. a1.ClientId = NewId()
  30. a1.UserId = NewId()
  31. a1.Code = NewId()
  32. a1.PreSave()
  33. a1.IsExpired()
  34. }
  35. func TestAuthIsValid(t *testing.T) {
  36. ad := AuthData{}
  37. if err := ad.IsValid(); err == nil {
  38. t.Fatal()
  39. }
  40. ad.ClientId = NewRandomString(28)
  41. if err := ad.IsValid(); err == nil {
  42. t.Fatal("Should have failed Client Id")
  43. }
  44. ad.ClientId = NewId()
  45. if err := ad.IsValid(); err == nil {
  46. t.Fatal()
  47. }
  48. ad.UserId = NewRandomString(28)
  49. if err := ad.IsValid(); err == nil {
  50. t.Fatal("Should have failed User Id")
  51. }
  52. ad.UserId = NewId()
  53. if err := ad.IsValid(); err == nil {
  54. t.Fatal()
  55. }
  56. ad.Code = NewRandomString(129)
  57. if err := ad.IsValid(); err == nil {
  58. t.Fatal("Should have failed Code to long")
  59. }
  60. ad.Code = ""
  61. if err := ad.IsValid(); err == nil {
  62. t.Fatal("Should have failed Code not set")
  63. }
  64. ad.Code = NewId()
  65. if err := ad.IsValid(); err == nil {
  66. t.Fatal()
  67. }
  68. ad.ExpiresIn = 0
  69. if err := ad.IsValid(); err == nil {
  70. t.Fatal("Should have failed invalid ExpiresIn")
  71. }
  72. ad.ExpiresIn = 1
  73. if err := ad.IsValid(); err == nil {
  74. t.Fatal()
  75. }
  76. ad.CreateAt = 0
  77. if err := ad.IsValid(); err == nil {
  78. t.Fatal("Should have failed Invalid Create At")
  79. }
  80. ad.CreateAt = 1
  81. if err := ad.IsValid(); err == nil {
  82. t.Fatal()
  83. }
  84. ad.State = NewRandomString(129)
  85. if err := ad.IsValid(); err == nil {
  86. t.Fatal("Should have failed invalid State")
  87. }
  88. ad.State = NewRandomString(128)
  89. if err := ad.IsValid(); err == nil {
  90. t.Fatal(err)
  91. }
  92. ad.Scope = NewRandomString(1025)
  93. if err := ad.IsValid(); err == nil {
  94. t.Fatal("Should have failed invalid Scope")
  95. }
  96. ad.Scope = NewRandomString(128)
  97. if err := ad.IsValid(); err == nil {
  98. t.Fatal()
  99. }
  100. ad.RedirectUri = ""
  101. if err := ad.IsValid(); err == nil {
  102. t.Fatal("Should have failed Redirect URI not set")
  103. }
  104. ad.RedirectUri = NewRandomString(28)
  105. if err := ad.IsValid(); err == nil {
  106. t.Fatal("Should have failed invalid URL")
  107. }
  108. ad.RedirectUri = NewRandomString(257)
  109. if err := ad.IsValid(); err == nil {
  110. t.Fatal("Should have failed invalid URL")
  111. }
  112. ad.RedirectUri = "http://example.com"
  113. if err := ad.IsValid(); err != nil {
  114. t.Fatal(err)
  115. }
  116. }