summaryrefslogtreecommitdiff
path: root/test/support/api_integration_test.rb
blob: 7942558584ecc7ed391cd8fc26ca2ba73a938edf (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
class ApiIntegrationTest < ActionDispatch::IntegrationTest

  DUMMY_TOKEN = Token.new
  RACK_ENV = {'HTTP_AUTHORIZATION' => %Q(Token token="#{DUMMY_TOKEN.to_s}")}

  def api_version
    2
  end

  def login(user = nil)
    @user ||= user ||= create_invited_user
    # DUMMY_TOKEN will be frozen. So let's use a dup
    @token ||= DUMMY_TOKEN.dup
    # make sure @token is up to date if it already exists
    @token.reload if @token.persisted?
    @token.user_id = @user.id
    @token.last_seen_at = Time.now
    @token.save
  end

  def create_invited_user(options = {})
    @testcode = InviteCode.new
    @testcode.save!
    options.reverse_merge! invite_code: @testcode.invite_code
    FactoryGirl.create :user, options
  end

  teardown do
    if @user && @user.persisted?
      @user.destroy_identities
      @user.reload.destroy
    end
    if @token && @token.persisted?
      @token.reload.destroy
    end
  end
end