Skip to content

Commit

Permalink
Merge branch 'master' into fix_user_merger
Browse files Browse the repository at this point in the history
  • Loading branch information
data-doge committed Apr 11, 2016
2 parents e07517d + f1234d9 commit 4014f9c
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 115 deletions.
6 changes: 3 additions & 3 deletions app/controllers/buckets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def update
end
end

api :POST, '/buckets/:id?target&funding_closes_at'
api :POST, '/buckets/:id'
def open_for_funding
bucket = Bucket.find(params[:id])
bucket.open_for_funding(target: params[:target], funding_closes_at: params[:funding_closes_at])
# BucketService.send_bucket_live_emails(bucket: bucket)
render status: 403, nothing: true and return unless bucket.is_editable_by?(current_user)
bucket.update(status: "live")
render json: [bucket]
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/contributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create
ContributionService.send_bucket_received_contribution_emails(contribution: contribution)
render json: [contribution]
else
render nothing: true, status: 422
render json: contribution.errors.full_messages, status: 422
end
end

Expand Down
12 changes: 0 additions & 12 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ def confirm_account
end
end

# can remove once user can start their own group
api :POST, '/users/invite_to_create_group?email'
def invite_to_create_group
user = User.create_with_confirmation_token(email: params[:email])
if user.valid?
UserMailer.join_cobudget_and_create_group_invite(user: user, inviter: current_user).deliver_later
render status: 200, nothing: true
else
render status: 409, nothing: true
end
end

api :POST, '/users?email'
def create
tmp_password = SecureRandom.hex
Expand Down
9 changes: 0 additions & 9 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ def notify_member_that_they_received_allocation(admin: , member: , group: , amou
subject: "#{admin.name} gave you funds to spend in #{@group.name}")
end

def join_cobudget_and_create_group_invite(user: , inviter:)
@user = user
@inviter = inviter
@redirect_url = "#{root_url}#/confirm_account?confirmation_token=#{@user.confirmation_token}&setup_group=true".html_safe
mail(to: @user.name_and_email,
from: "Cobudget Accounts <[email protected]>",
subject: "#{inviter.name} invited you to create a new group on Cobudget")
end

def notify_funder_that_bucket_was_deleted(funder: , bucket: )
@bucket = bucket
@group = @bucket.group
Expand Down
14 changes: 5 additions & 9 deletions app/models/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ def formatted_target
Money.new(target * 100, currency_code).format
end

# funding_closes_at, need to think more about the implications of setting this
def open_for_funding(target:, funding_closes_at:)
update(target: target, status: "live", funding_closes_at: funding_closes_at, live_at: Time.now.utc)
end

# TODO: eventually bring this stuff onto the client side
def num_of_contributors
contributions.map { |c| c.user_id }.uniq.length
end
Expand Down Expand Up @@ -84,9 +78,11 @@ def is_editable_by?(member)

private
def set_timestamp_if_status_updated
case self.status
when "live" then self.live_at = Time.now.utc
when "funded" then self.funded_at = Time.now.utc
if status_changed?
case self.status
when "live" then self.live_at = Time.now.utc
when "funded" then self.funded_at = Time.now.utc
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/contribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Contribution < ActiveRecord::Base
validates :amount, numericality: { greater_than: 0 }
validate :amount_cannot_overdraft_member

before_save :lower_amount_if_exceeds_target
before_save :lower_amount_if_exceeds_bucket_target
after_save :update_bucket_status_if_funded

def formatted_amount
Money.new(amount * 100, currency_code).format
end

private
def lower_amount_if_exceeds_target
def lower_amount_if_exceeds_bucket_target
if bucket.total_contributions + self.amount > bucket.target
self.amount = bucket.target - bucket.total_contributions
end
Expand All @@ -29,7 +29,8 @@ def update_bucket_status_if_funded

def amount_cannot_overdraft_member
membership = Membership.find_by(member_id: user_id, group_id: bucket.group_id)
if membership.raw_balance - amount < 0
@balance ||= membership.raw_balance
if @balance - self.amount < 0
errors.add(:amount, "amount cannot overdraft member")
end
end
Expand Down
11 changes: 0 additions & 11 deletions app/views/user_mailer/join_cobudget_and_create_group_invite.erb

This file was deleted.

13 changes: 2 additions & 11 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,15 @@
# config.action_mailer.raise_delivery_errors = false

config.action_mailer.default_url_options = { :host => ENV['CANONICAL_HOST'] }

# config.action_mailer.smtp_settings = {
# :address => 'smtp.sendgrid.net',
# :port => '587',
# :authentication => :plain,
# :user_name => ENV['SENDGRID_USERNAME'],
# :password => ENV['SENDGRID_PASSWORD'],
# :domain => 'heroku.com',
# :enable_starttls_auto => true
# }

config.action_mailer.smtp_settings = {
:address => ENV['SMTP_SERVER'],
:port => ENV['SMTP_PORT'],
:authentication => :plain,
:user_name => ENV['SMTP_USERNAME'],
:password => ENV['SMTP_PASSWORD'],
:domain => ENV['SMTP_DOMAIN']
:domain => ENV['SMTP_DOMAIN'],
:enable_starttls_auto => true
}

end
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
post :request_password_reset
post :reset_password
post :update_profile
post :invite_to_create_group
post :update_password
get :me
end
Expand Down
85 changes: 83 additions & 2 deletions spec/controllers/buckets_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,90 @@
require 'rails_helper'

RSpec.describe BucketsController, type: :controller do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:bucket) { create(:bucket, group: group, status: 'draft') }

after { ActionMailer::Base.deliveries.clear }

describe "#open_for_funding" do
describe "permissions" do
context "user signed in" do
before { request.headers.merge!(user.create_new_auth_token) }

context "user is group member" do
before { group.add_member(user) }

context "user is admin" do
before { group.add_admin(user) }

it "returns http status 'success'" do
post :open_for_funding, { id: bucket.id }
expect(response).to have_http_status(:success)
end
end

context "user is bucket author" do
before { bucket.update(user: user) }

it "returns http status 'success'" do
post :open_for_funding, { id: bucket.id }
expect(response).to have_http_status(:success)
end
end

context "user is neither admin nor bucket author" do
it "returns http status 'forbidden'" do
post :open_for_funding, { id: bucket.id }
expect(response).to have_http_status(:forbidden)
end
end
end

context "user is not group member" do
it "returns http status 'forbidden'" do
post :open_for_funding, { id: bucket.id }
expect(response).to have_http_status(:forbidden)
end
end
end

context "user not signed in" do
it "returns http status 'unauthorized'" do
post :open_for_funding, { id: bucket.id }
expect(response).to have_http_status(:unauthorized)
end
end
end

describe "behavior" do
before do
request.headers.merge!(user.create_new_auth_token)
group.add_admin(user)
end

context "bucket active" do
before do
post :open_for_funding, { id: bucket.id }
bucket.reload
end

it "updates bucket status to live" do
expect(bucket.status).to eq("live")
end

it "sets live_at on bucket" do
expect(bucket.live_at).not_to be_nil
end

it "returns bucket as json" do
expect(parsed(response)["buckets"][0]["id"]).to eq(bucket.id)
end
end
end
end

describe "#update" do
let!(:bucket) { create(:bucket, status: 'draft') }
let!(:group) { bucket.group }
let(:bucket_params) {{
id: bucket.id,
bucket: {
Expand Down
14 changes: 10 additions & 4 deletions spec/controllers/contributions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@

RSpec.describe ContributionsController, type: :controller do
describe "#create" do
let!(:user) { create(:user) }
let!(:group) { create(:group) }
let!(:bucket) { create(:bucket, group: group) }

context "user signed in" do
before { request.headers.merge!(user.create_new_auth_token) }

context "user is member of group" do
let!(:membership) { create(:membership, member: user, group: group) }

before do
make_user_group_member
bucket.update(target: 100, group: group)
bucket.update(target: 100)
bucket.update(status: 'live')
end

context "contribution amount does not exceed contributor's balance" do
before do
create(:allocation, user: user, group: group, amount: 1000)
create(:allocation, user: user, group: group, amount: 100)
end

context "contribution amount does not exceed bucket's funding target" do
before do
contribution_params = {
bucket_id: bucket.id,
amount: 50
amount: 100
}
post :create, {contribution: contribution_params}
@contribution = Contribution.find_by(contribution_params)
Expand Down Expand Up @@ -54,6 +59,7 @@

context "contribution amount exceeds bucket's funding target" do
it "contribution amount is decreased to exactly meet the bucket's funding target" do
create(:allocation, user: user, group: group, amount: 1000)
contribution_params = {
bucket_id: bucket.id,
amount: 150
Expand Down
49 changes: 0 additions & 49 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,55 +160,6 @@
end
end

describe "#invite_to_create_group" do
before do
make_user_group_admin
request.headers.merge!(user.create_new_auth_token)
end

context "specified email does not yet exist in DB" do
before do
params = { email: Faker::Internet.email }
post :invite_to_create_group, params
@new_user = User.find_by(params)
@sent_email = ActionMailer::Base.deliveries.first
end

after do
ActionMailer::Base.deliveries.clear
end

it "creates a new user with confirmation token" do
expect(@new_user).to be_truthy
expect(@new_user.confirmation_token).to be_truthy
end

it "sends email to user with link to confirm account page containing confirmation_token and boolean setup_group set to true" do
expect(@sent_email.to).to eq([@new_user.email])
expected_url = "/#/confirm_account?confirmation_token=#{@new_user.confirmation_token}&setup_group=true"
expect(@sent_email.body.to_s).to include(expected_url)
end
end

context "specified email already exists in DB" do
before do
make_user_group_admin
request.headers.merge!(user.create_new_auth_token)
create(:user, email: "[email protected]")
@params = {email: "[email protected]"}
post :invite_to_create_group, @params
end

it "does not create user" do
expect(User.where(@params).length).to eq(1)
end

it "returns http status conflict" do
expect(response).to have_http_status(:conflict)
end
end
end

describe "#update_profile" do
context "user signed in" do
before do
Expand Down
18 changes: 18 additions & 0 deletions spec/models/bucket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@
end

describe "#set_timestamp_if_status_updated" do
context "live bucket updated, but not status" do
it "does not set timestamps" do
current_time = DateTime.now.utc
Timecop.freeze(current_time - 1.hour) do
bucket.update(status: "live")
end

Timecop.freeze(current_time) do
bucket.update(description: "new description ayyyy")
end

Timecop.return

bucket.reload
expect(bucket.live_at).to eq(current_time - 1.hour)
end
end

context "status updated to 'live'" do
it "sets live_at" do
bucket.update(status: 'live')
Expand Down

0 comments on commit 4014f9c

Please sign in to comment.