Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Merge b199d61 into a4d3ea5
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Harris committed Jun 6, 2016
2 parents a4d3ea5 + b199d61 commit 7049ac4
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 2 deletions.
16 changes: 16 additions & 0 deletions app/controllers/admin/user_reports_controller.rb
@@ -0,0 +1,16 @@
class Admin::UserReportsController < ApplicationController
def index
@users = User.all

respond_to do |format|
format.csv do
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = "attachment"
send_data(
UserExport.new.export_csv,
filename: "micropurchase_users.csv"
)
end
end
end
end
39 changes: 39 additions & 0 deletions app/models/user_export.rb
@@ -0,0 +1,39 @@
require 'csv'

class UserExport
class Error < StandardError; end

def initialize
@users = User.all
end

def export_csv
CSV.generate do |csv|
csv << header_values

users.each do |user|
csv << data_values(UserPresenter.new(user))
end
end
end

private

attr_reader :users

def header_values
["Name",
"Email Address",
"Github ID",
"In SAM?",
"Small Business"]
end

def data_values(user)
[user.name,
user.email,
user.github_login,
user.sam_status_label,
user.small_business_label]
end
end
8 changes: 6 additions & 2 deletions app/presenters/user_presenter.rb
Expand Up @@ -8,7 +8,7 @@ def admin?
end

def in_sam?
model.sam_status
model.sam_accepted?
end

def sam_status_message_for(flash)
Expand All @@ -31,8 +31,12 @@ def nav_drawer_submenu_partial
"components/user_nav_drawer_submenu"
end

def sam_status_label
in_sam? ? 'Yes' : 'No'
end

def small_business_label
if model.sam_accepted?
if in_sam?
small_business? ? 'Yes' : 'No'
else
'N/A'
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -28,6 +28,7 @@
resources :auctions
resources :users, only: [:index, :edit, :update, :show]
resources :auction_reports, only: [:show]
resources :user_reports, only: [:index]
end

get '/auth/:provider/callback', to: 'authentications#create'
Expand Down
17 changes: 17 additions & 0 deletions spec/controllers/admin/user_reports_controller_spec.rb
@@ -0,0 +1,17 @@
require 'rails_helper'

RSpec.describe Admin::UserReportsController do
describe '#index' do
context 'when called by an admin user' do
it 'should return a CSV' do
user = create(:admin_user)
10.times { create(:user) }

get :index, { format: 'csv' }, user_id: user.id

expect(response.code).to eq('200')
expect(response.header['Content-Type']).to include('text/csv')
end
end
end
end
1 change: 1 addition & 0 deletions spec/factories/users.rb
Expand Up @@ -6,6 +6,7 @@
duns_number { Faker::Company.duns_number }
name { Faker::Name.name }
email
github_login 'ghlogin'
github_id 123_456
sam_status :duns_blank
credit_card_form_url 'https://some-website.com/pay'
Expand Down
53 changes: 53 additions & 0 deletions spec/models/user_export_spec.rb
@@ -0,0 +1,53 @@
require 'rails_helper'

RSpec.describe UserExport do
describe '#export_csv' do
context 'for a user not in SAM yet' do
it 'should return the correct CSV' do
user = FactoryGirl.create(:user, sam_status: :sam_pending, small_business: false)

export = UserExport.new.export_csv

parsed = CSV.parse(export)

expect(parsed[1][0]).to include(user.name)
expect(parsed[1][1]).to include(user.email)
expect(parsed[1][2]).to include(user.github_login)
expect(parsed[1][3]).to include('No')
expect(parsed[1][4]).to include('N/A')
end
end

context 'for a small-business user in SAM' do
it 'should return the correct CSV' do
user = FactoryGirl.create(:user, :small_business)

export = UserExport.new.export_csv

parsed = CSV.parse(export)

expect(parsed[1][0]).to include(user.name)
expect(parsed[1][1]).to include(user.email)
expect(parsed[1][2]).to include(user.github_login)
expect(parsed[1][3]).to include('Yes')
expect(parsed[1][4]).to include('Yes')
end
end

context 'for a user in SAM who is not a small business' do
it 'should return the correct CSV' do
user = FactoryGirl.create(:user, :not_small_business)

export = UserExport.new.export_csv

parsed = CSV.parse(export)

expect(parsed[1][0]).to include(user.name)
expect(parsed[1][1]).to include(user.email)
expect(parsed[1][2]).to include(user.github_login)
expect(parsed[1][3]).to include('Yes')
expect(parsed[1][4]).to include('No')
end
end
end
end
46 changes: 46 additions & 0 deletions spec/presenters/user_presenter_spec.rb
@@ -0,0 +1,46 @@
require 'rails_helper'

RSpec.describe UserPresenter do
describe '#in_sam?' do
it "should return true when the user's sam_status is sam_accepted" do
user = UserPresenter.new(create(:user, sam_status: :sam_accepted))
expect(user).to be_in_sam
expect(user.sam_status_label).to eq('Yes')
end

it "should return false when the user's sam_status is sam_pending" do
user = UserPresenter.new(create(:user, sam_status: :sam_pending))
expect(user).to_not be_in_sam
expect(user.sam_status_label).to eq('No')
end

it "should return false when the user's sam_status is sam_rejected" do
user = UserPresenter.new(create(:user, sam_status: :sam_rejected))
expect(user).to_not be_in_sam
expect(user.sam_status_label).to eq('No')
end

it "should return false when the user's sam_status is duns_blank" do
user = UserPresenter.new(create(:user, sam_status: :duns_blank))
expect(user).to_not be_in_sam
expect(user.sam_status_label).to eq('No')
end
end

describe '#small_business_label' do
it 'should return "N/A" if the user is not in_sam' do
user = UserPresenter.new(create(:user, sam_status: :sam_pending, small_business: true))
expect(user.small_business_label).to eq("N/A")
end

it 'should return "Yes" if the user is in SAM and a small business' do
user = UserPresenter.new(create(:user, :small_business))
expect(user.small_business_label).to eq("Yes")
end

it 'should return "No" if the user is in SAM and not a small business' do
user = UserPresenter.new(create(:user, :not_small_business))
expect(user.small_business_label).to eq("No")
end
end
end

0 comments on commit 7049ac4

Please sign in to comment.