require 'spec_helper'

describe ApiPosaCardsController, type: :controller do
  render_views

  let (:password) {'abcdef'}
  let (:posa_card) {FactoryGirl.create(:posa_card)}
  let (:owner) {FactoryGirl.create(:user)}
  let (:user) {FactoryGirl.create(:user)}
  let (:retailer) {FactoryGirl.create(:retailer, user: owner)}

  let (:authorization) { 'Basic ' + Base64::encode64("#{password}:#{password}") }



  before(:each) do
    retailer.update_from_params({password:password})
  end

  describe "activate" do
    it "works" do
      request.headers['HTTP_AUTHORIZATION'] = authorization
      get :activate, slug: retailer.slug, code: posa_card.code
      response.should be_success
      JSON.parse(response.body)['id'].should eql posa_card.id
      posa_card.reload
      posa_card.activated_at.should_not be_nil
      posa_card.retailer.should eql retailer
    end
  end

  describe "claim" do
    it "works" do
      controller.current_user = user
      posa_card.activate(retailer)
      get :claim, code: posa_card.code
      response.should be_success
      JSON.parse(response.body)['id'].should eql posa_card.id
      posa_card.reload
      posa_card.claimed_at.should_not be_nil
      posa_card.user.should eql user
    end
  end
end
