token_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. require 'rails_helper'
  2. RSpec.describe Token, type: :model do
  3. subject(:token) { create(:password_reset_token) }
  4. describe '.check' do
  5. context 'with name and action matching existing token' do
  6. it 'returns the token’s user' do
  7. expect(described_class.check(action: token.action, name: token.name)).to eq(token.user)
  8. end
  9. end
  10. context 'with invalid name' do
  11. it 'returns nil' do
  12. expect(described_class.check(action: token.action, name: '1NV4L1D')).to be(nil)
  13. end
  14. end
  15. context 'with invalid action' do
  16. it 'returns nil' do
  17. expect(described_class.check(action: 'PasswordReset_NotExisting', name: token.name)).to be(nil)
  18. end
  19. end
  20. describe 'persistence handling' do
  21. context 'for persistent token' do
  22. subject(:token) { create(:ical_token, persistent: true, created_at: created_at) }
  23. context 'at any time' do
  24. let(:created_at) { 1.month.ago }
  25. it 'returns the token’s user' do
  26. expect(described_class.check(action: token.action, name: token.name)).to eq(token.user)
  27. end
  28. it 'does not delete the token' do
  29. token # create token
  30. expect { described_class.check(action: token.action, name: token.name) }
  31. .not_to change(described_class, :count)
  32. end
  33. end
  34. end
  35. context 'for non-persistent token' do
  36. subject(:token) { create(:password_reset_token, persistent: false, created_at: created_at) }
  37. context 'less than one day after creation' do
  38. let(:created_at) { 1.day.ago + 5 }
  39. it 'returns the token’s user' do
  40. expect(described_class.check(action: token.action, name: token.name)).to eq(token.user)
  41. end
  42. it 'does not delete the token' do
  43. token # create token
  44. expect { described_class.check(action: token.action, name: token.name) }
  45. .not_to change(described_class, :count)
  46. end
  47. end
  48. context 'at least one day after creation' do
  49. let(:created_at) { 1.day.ago }
  50. it 'returns nil' do
  51. expect(described_class.check(action: token.action, name: token.name)).to be(nil)
  52. end
  53. it 'deletes the token' do
  54. token # create token
  55. expect { described_class.check(action: token.action, name: token.name) }
  56. .to change(described_class, :count).by(-1)
  57. end
  58. end
  59. end
  60. end
  61. describe 'permission matching' do
  62. subject(:token) { create(:api_token, user: agent, preferences: preferences) }
  63. let(:agent) { create(:agent_user) }
  64. let(:preferences) { { permission: %w[admin ticket.agent] } } # agent has no access to admin.*
  65. context 'with a permission shared by both token.user and token.preferences' do
  66. it 'returns token.user' do
  67. expect(described_class.check(action: token.action, name: token.name, permission: 'ticket.agent')).to eq(agent)
  68. end
  69. end
  70. context 'with the child of a permission shared by both token.user and token.preferences' do
  71. it 'returns token.user' do
  72. expect(described_class.check(action: token.action, name: token.name, permission: 'ticket.agent.foo')).to eq(agent)
  73. end
  74. end
  75. context 'with the parent of a permission shared by both token.user and token.preferences' do
  76. it 'returns nil' do
  77. expect(described_class.check(action: token.action, name: token.name, permission: 'ticket')).to be(nil)
  78. end
  79. end
  80. context 'with a permission in token.preferences, but not on token.user' do
  81. it 'returns nil' do
  82. expect(described_class.check(action: token.action, name: token.name, permission: 'admin')).to be(nil)
  83. end
  84. end
  85. context 'with a permission not in token.preferences, but on token.user' do
  86. it 'returns nil' do
  87. expect(described_class.check(action: token.action, name: token.name, permission: 'cti.agent')).to be(nil)
  88. end
  89. end
  90. context 'with non-existent permission' do
  91. it 'returns nil' do
  92. expect(described_class.check(action: token.action, name: token.name, permission: 'foo')).to be(nil)
  93. end
  94. end
  95. context 'with multiple permissions, where at least one is shared by both token.user and token.preferences' do
  96. it 'returns token.user' do
  97. expect(described_class.check(action: token.action, name: token.name, permission: %w[foo ticket.agent])).to eq(agent)
  98. end
  99. end
  100. end
  101. end
  102. describe 'Attributes:' do
  103. describe '#persistent' do
  104. context 'when not set on creation' do
  105. subject(:token) { described_class.create(action: 'foo', user_id: User.first.id) }
  106. it 'defaults to nil' do
  107. expect(token.persistent).to be(nil)
  108. end
  109. end
  110. end
  111. end
  112. end