This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |