signature_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. require "rails_helper"
  2. describe Signature do
  3. before(:each) do
  4. @attr = {
  5. petition_id: 1,
  6. first_name: "Save Kittens",
  7. last_name: "Save kittens in great detail",
  8. email: "johnsmith@eff.org",
  9. country_code: "US",
  10. zipcode: "94109",
  11. street_address: "815 Eddy Street",
  12. city: "San Francisco",
  13. state: "CA",
  14. anonymous: false
  15. }
  16. end
  17. it "should create a new instance given a valid attribute" do
  18. Signature.create!(@attr)
  19. end
  20. it "should reject spammy emails" do
  21. invalid_email = @attr.merge(email: "a@b")
  22. expect {
  23. Signature.create!(invalid_email)
  24. }.to raise_error ActiveRecord::RecordInvalid
  25. end
  26. it "should impose an arbitrary opinion as to whether a string of text may refer to a country" do
  27. # note: it is my personal belief that there is no such thing as a country/ nation =)
  28. arbitrarily_invalid_opinion = @attr.merge(country_code: "laserland")
  29. expect {
  30. Signature.create!(arbitrarily_invalid_opinion)
  31. }.to raise_error ActiveRecord::RecordInvalid
  32. end
  33. it "should reject long zipcodes" do
  34. long_zip = @attr.merge(zipcode: "9" * 13)
  35. expect {
  36. Signature.create!(long_zip)
  37. }.to raise_error ActiveRecord::RecordInvalid
  38. end
  39. describe ".filter" do
  40. let(:jon) { Signature.create!(@attr.merge(email: "xx@example.com", first_name: "Jon", last_name: "A")) }
  41. let(:jan) { Signature.create!(@attr.merge(email: "xy@example.com", first_name: "Jan", last_name: "B")) }
  42. let(:jeb) { Signature.create!(@attr.merge(email: "wz@example.com", first_name: "Jeb", last_name: "C")) }
  43. let(:jen) { Signature.create!(@attr.merge(email: "wv@example.com", first_name: "Jen", last_name: "D")) }
  44. let(:all_signatures) { [jon, jan, jeb, jen] }
  45. it "should return .all when query is blank" do
  46. expect(Signature.filter(nil)).to contain_exactly(*all_signatures)
  47. expect(Signature.filter("")).to contain_exactly(*all_signatures)
  48. expect(Signature.filter(" \t")).to contain_exactly(*all_signatures)
  49. end
  50. it "should return signatures with matching email (case insensitive)" do
  51. expect(Signature.filter("example.com")).to contain_exactly(*all_signatures)
  52. expect(Signature.filter("EXAMPLE")).to contain_exactly(*all_signatures)
  53. expect(Signature.filter("w")).to contain_exactly(jeb, jen)
  54. expect(Signature.filter("xx")).to contain_exactly(jon)
  55. end
  56. it "should return signatures with matching names (case insensitive)" do
  57. expect(Signature.filter("J")).to contain_exactly(*all_signatures)
  58. expect(Signature.filter("Jon")).to contain_exactly(jon)
  59. expect(Signature.filter("Jan B")).to contain_exactly(jan)
  60. expect(Signature.filter("Jeb c")).to contain_exactly(jeb)
  61. end
  62. end
  63. end