PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/oauth_test.go

https://gitlab.com/unofficial-mirrors/mattermost-platform
Go | 94 lines | 72 code | 20 blank | 2 comment | 18 complexity | 1f05181742338b50c6dcc1e806473f0c MD5 | raw file
  1. // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
  2. // See License.txt for license information.
  3. package app
  4. import (
  5. "testing"
  6. "github.com/mattermost/mattermost-server/model"
  7. )
  8. func TestOAuthRevokeAccessToken(t *testing.T) {
  9. th := Setup()
  10. defer th.TearDown()
  11. if err := th.App.RevokeAccessToken(model.NewRandomString(16)); err == nil {
  12. t.Fatal("Should have failed bad token")
  13. }
  14. session := &model.Session{}
  15. session.CreateAt = model.GetMillis()
  16. session.UserId = model.NewId()
  17. session.Token = model.NewId()
  18. session.Roles = model.SYSTEM_USER_ROLE_ID
  19. session.SetExpireInDays(1)
  20. session, _ = th.App.CreateSession(session)
  21. if err := th.App.RevokeAccessToken(session.Token); err == nil {
  22. t.Fatal("Should have failed does not have an access token")
  23. }
  24. accessData := &model.AccessData{}
  25. accessData.Token = session.Token
  26. accessData.UserId = session.UserId
  27. accessData.RedirectUri = "http://example.com"
  28. accessData.ClientId = model.NewId()
  29. accessData.ExpiresAt = session.ExpiresAt
  30. if result := <-th.App.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil {
  31. t.Fatal(result.Err)
  32. }
  33. if err := th.App.RevokeAccessToken(accessData.Token); err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. func TestOAuthDeleteApp(t *testing.T) {
  38. th := Setup()
  39. defer th.TearDown()
  40. th.App.Config().ServiceSettings.EnableOAuthServiceProvider = true
  41. a1 := &model.OAuthApp{}
  42. a1.CreatorId = model.NewId()
  43. a1.Name = "TestApp" + model.NewId()
  44. a1.CallbackUrls = []string{"https://nowhere.com"}
  45. a1.Homepage = "https://nowhere.com"
  46. var err *model.AppError
  47. a1, err = th.App.CreateOAuthApp(a1)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. session := &model.Session{}
  52. session.CreateAt = model.GetMillis()
  53. session.UserId = model.NewId()
  54. session.Token = model.NewId()
  55. session.Roles = model.SYSTEM_USER_ROLE_ID
  56. session.IsOAuth = true
  57. session.SetExpireInDays(1)
  58. session, _ = th.App.CreateSession(session)
  59. accessData := &model.AccessData{}
  60. accessData.Token = session.Token
  61. accessData.UserId = session.UserId
  62. accessData.RedirectUri = "http://example.com"
  63. accessData.ClientId = a1.Id
  64. accessData.ExpiresAt = session.ExpiresAt
  65. if result := <-th.App.Srv.Store.OAuth().SaveAccessData(accessData); result.Err != nil {
  66. t.Fatal(result.Err)
  67. }
  68. if err := th.App.DeleteOAuthApp(a1.Id); err != nil {
  69. t.Fatal(err)
  70. }
  71. if _, err := th.App.GetSession(session.Token); err == nil {
  72. t.Fatal("should not get session from cache or db")
  73. }
  74. }