1
0

user_spec.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. require 'rails_helper'
  2. describe User do
  3. let(:bob) { users(:bob) }
  4. describe "validations" do
  5. describe "invitation_code" do
  6. context "when configured to use invitation codes" do
  7. before do
  8. allow(User).to receive(:using_invitation_code?) {true}
  9. end
  10. it "only accepts valid invitation codes" do
  11. User::INVITATION_CODES.each do |v|
  12. should allow_value(v).for(:invitation_code)
  13. end
  14. end
  15. it "can reject invalid invitation codes" do
  16. %w['foo', 'bar'].each do |v|
  17. should_not allow_value(v).for(:invitation_code)
  18. end
  19. end
  20. it "requires no authentication code when requires_no_invitation_code! is called" do
  21. u = User.new(username: 'test', email: 'test@test.com', password: '12345678', password_confirmation: '12345678')
  22. u.requires_no_invitation_code!
  23. expect(u).to be_valid
  24. end
  25. end
  26. context "when configured not to use invitation codes" do
  27. before do
  28. allow(User).to receive(:using_invitation_code?) {false}
  29. end
  30. it "skips this validation" do
  31. %w['foo', 'bar', nil, ''].each do |v|
  32. should allow_value(v).for(:invitation_code)
  33. end
  34. end
  35. end
  36. end
  37. end
  38. context '#deactivate!' do
  39. it "deactivates the user and all her agents" do
  40. agent = agents(:jane_website_agent)
  41. users(:jane).deactivate!
  42. agent.reload
  43. expect(agent.deactivated).to be_truthy
  44. expect(users(:jane).deactivated_at).not_to be_nil
  45. end
  46. end
  47. context '#activate!' do
  48. before do
  49. users(:bob).deactivate!
  50. end
  51. it 'activates the user and all his agents' do
  52. agent = agents(:bob_website_agent)
  53. users(:bob).activate!
  54. agent.reload
  55. expect(agent.deactivated).to be_falsy
  56. expect(users(:bob).deactivated_at).to be_nil
  57. end
  58. end
  59. context '#undefined_agent_types' do
  60. it 'returns an empty array when no agents are undefined' do
  61. expect(bob.undefined_agent_types).to be_empty
  62. end
  63. it 'returns the undefined agent types' do
  64. agent = agents(:bob_website_agent)
  65. agent.update_attribute(:type, 'Agents::UndefinedAgent')
  66. expect(bob.undefined_agent_types).to match_array(['Agents::UndefinedAgent'])
  67. end
  68. end
  69. context '#undefined_agents' do
  70. it 'returns an empty array when no agents are undefined' do
  71. expect(bob.undefined_agents).to be_empty
  72. end
  73. it 'returns the undefined agent types' do
  74. agent = agents(:bob_website_agent)
  75. agent.update_attribute(:type, 'Agents::UndefinedAgent')
  76. expect(bob.undefined_agents).not_to be_empty
  77. expect(bob.undefined_agents.first).to be_a(Agent)
  78. end
  79. end
  80. end