summaryrefslogtreecommitdiff
path: root/test/unit/invite_code_validator_test.rb
blob: 62eeae6f8c3dd98b377be693872d71e9f46182ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'test_helper'

class InviteCodeValidatorTest < ActiveSupport::TestCase
  test "user should not be created with invalid invite code" do
    with_config invite_required: true do
    invalid_user = FactoryGirl.build(:user)

    assert !invalid_user.valid?
    end
  end

  test "user should be created with valid invite code" do
    valid_user = FactoryGirl.build(:user)
    valid_code = InviteCode.create
    valid_user.invite_code = valid_code.invite_code

    assert valid_user.valid?
  end

  test "trying to create a user with invalid invite code should add error" do
    with_config invite_required: true do
    invalid_user = FactoryGirl.build(:user, :invite_code => "a non-existent code")

    invalid_user.valid?

    errors = {invite_code: ["This is not a valid code"]}
    assert_equal errors, invalid_user.errors.messages
    end
  end


  test "Invite count >= invite max uses is not accepted for new account signup" do
    validator = InviteCodeValidator.new nil

    user_code = InviteCode.new
    user_code.invite_count = 1
    user_code.save

    user = FactoryGirl.build :user
    user.invite_code = user_code.invite_code

    validator.validate(user)

    assert_equal ["This code has already been used"], user.errors[:invite_code]

  end

  test "Invite count < invite max uses is accepted for new account signup" do
    validator = InviteCodeValidator.new nil

    user_code = InviteCode.create
    user_code.save

    user = FactoryGirl.build :user
    user.invite_code = user_code.invite_code

    validator.validate(user)

    assert_equal [], user.errors[:invite_code]
  end

  test "Invite count 0 is accepted for new account signup" do
    validator = InviteCodeValidator.new nil

    user_code = InviteCode.create

    user = FactoryGirl.build :user
    user.invite_code = user_code.invite_code

    validator.validate(user)

    assert_equal [], user.errors[:invite_code]
  end

  test "There is an error message if the invite code does not exist" do
    validator = InviteCodeValidator.new nil

    user = FactoryGirl.build :user
    user.invite_code = "wrongcode"

    validator.validate(user)

    assert_equal ["This is not a valid code"], user.errors[:invite_code]
  end

end