PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/model/access_test.go

https://gitlab.com/unofficial-mirrors/mattermost-platform
Go | 97 lines | 75 code | 20 blank | 2 comment | 30 complexity | 8ded583ee59288600cce724eb22cc52a 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 TestAccessJson(t *testing.T) {
  9. a1 := AccessData{}
  10. a1.ClientId = NewId()
  11. a1.UserId = NewId()
  12. a1.Token = NewId()
  13. a1.RefreshToken = NewId()
  14. json := a1.ToJson()
  15. ra1 := AccessDataFromJson(strings.NewReader(json))
  16. if a1.Token != ra1.Token {
  17. t.Fatal("tokens didn't match")
  18. }
  19. }
  20. func TestAccessIsValid(t *testing.T) {
  21. ad := AccessData{}
  22. if err := ad.IsValid(); err == nil {
  23. t.Fatal()
  24. }
  25. ad.ClientId = NewRandomString(28)
  26. if err := ad.IsValid(); err == nil {
  27. t.Fatal("Should have failed Client Id")
  28. }
  29. ad.ClientId = ""
  30. if err := ad.IsValid(); err == nil {
  31. t.Fatal("Should have failed Client Id")
  32. }
  33. ad.ClientId = NewId()
  34. if err := ad.IsValid(); err == nil {
  35. t.Fatal()
  36. }
  37. ad.UserId = NewRandomString(28)
  38. if err := ad.IsValid(); err == nil {
  39. t.Fatal("Should have failed User Id")
  40. }
  41. ad.UserId = ""
  42. if err := ad.IsValid(); err == nil {
  43. t.Fatal("Should have failed User Id")
  44. }
  45. ad.UserId = NewId()
  46. if err := ad.IsValid(); err == nil {
  47. t.Fatal("should have failed")
  48. }
  49. ad.Token = NewRandomString(22)
  50. if err := ad.IsValid(); err == nil {
  51. t.Fatal("Should have failed Token")
  52. }
  53. ad.Token = NewId()
  54. if err := ad.IsValid(); err == nil {
  55. t.Fatal()
  56. }
  57. ad.RefreshToken = NewRandomString(28)
  58. if err := ad.IsValid(); err == nil {
  59. t.Fatal("Should have failed Refresh Token")
  60. }
  61. ad.RefreshToken = NewId()
  62. if err := ad.IsValid(); err == nil {
  63. t.Fatal()
  64. }
  65. ad.RedirectUri = ""
  66. if err := ad.IsValid(); err == nil {
  67. t.Fatal("Should have failed Redirect URI not set")
  68. }
  69. ad.RedirectUri = NewRandomString(28)
  70. if err := ad.IsValid(); err == nil {
  71. t.Fatal("Should have failed invalid URL")
  72. }
  73. ad.RedirectUri = "http://example.com"
  74. if err := ad.IsValid(); err != nil {
  75. t.Fatal(err)
  76. }
  77. }