jam-cloud/web/spec/controllers/api_users_controller_spec.rb

434 lines
14 KiB
Ruby

require 'spec_helper'
require 'webmock/rspec'
describe ApiUsersController, type: :controller do
render_views
let (:user) { FactoryGirl.create(:user) }
let (:conn) { FactoryGirl.create(:connection, user: user, last_jam_audio_latency: 5) }
let (:jam_track) { FactoryGirl.create(:jam_track)}
before(:each) do
controller.current_user = user
end
describe "redeem_giftcard" do
let!(:gift_card) {FactoryGirl.create(:gift_card)}
it "can succeed" do
post :redeem_giftcard, id:user.id, gift_card: gift_card.code, format:'json'
response.should be_success
user.reload
gift_card.reload
user.gift_cards.should eq([gift_card])
user.gifted_jamtracks.should eq(5)
gift_card.user.should eq(user)
end
it "indicates if you've redeemed it" do
gift_card.user = user
gift_card.save!
post :redeem_giftcard, id:user.id, gift_card: gift_card.code, format:'json'
response.status.should eq(422)
error_data = JSON.parse(response.body)
error_data['errors']['gift_card'].should eq(["already redeemed by you"])
user.reload
gift_card.reload
user.gift_cards.should eq([gift_card])
user.gifted_jamtracks.should eq(5)
gift_card.user.should eq(user)
end
it "indicates if someone else has redeemed it" do
user2 = FactoryGirl.create(:user)
gift_card.user = user2
gift_card.save!
post :redeem_giftcard, id:user.id, gift_card: gift_card.code, format:'json'
response.status.should eq(422)
error_data = JSON.parse(response.body)
error_data['errors']['gift_card'].should eq(["already redeemed by another"])
user.reload
gift_card.reload
user.gift_cards.should eq([])
user.gifted_jamtracks.should eq(0)
gift_card.user.should eq(user2)
end
it "marks free shopping cart item as free" do
# sort of a 'do nothing' really
cart1 = ShoppingCart.add_jam_track_to_cart(user, jam_track)
cart1.marked_for_redeem.should eq(1)
post :redeem_giftcard, id:user.id, gift_card: gift_card.code, format:'json'
response.should be_success
user.reload
gift_card.reload
user.gift_cards.should eq([gift_card])
user.gifted_jamtracks.should eq(5)
gift_card.user.should eq(user)
cart1.reload
cart1.marked_for_redeem.should eq(1)
end
it "marks non-free shopping cart item as free" do
# sort of a 'do nothing' really
user.has_redeemable_jamtrack = false
user.save!
cart1 = ShoppingCart.add_jam_track_to_cart(user, jam_track)
cart1.marked_for_redeem.should eq(0)
post :redeem_giftcard, id:user.id, gift_card: gift_card.code, format:'json'
response.should be_success
user.reload
gift_card.reload
user.gift_cards.should eq([gift_card])
user.gifted_jamtracks.should eq(5)
gift_card.user.should eq(user)
cart1.reload
cart1.marked_for_redeem.should eq(1)
end
it "leaves shopping cart alone if too many items in it for size of new gift card" do
# sort of a 'do nothing' really
user.has_redeemable_jamtrack = false
user.save!
11.times do |i|
jamtrack = FactoryGirl.create(:jam_track)
cart1 = ShoppingCart.add_jam_track_to_cart(user, jamtrack)
cart1.marked_for_redeem.should eq(0)
end
post :redeem_giftcard, id:user.id, gift_card: gift_card.code, format:'json'
response.should be_success
user.reload
gift_card.reload
user.gift_cards.should eq([gift_card])
user.gifted_jamtracks.should eq(5)
gift_card.user.should eq(user)
user.shopping_carts.each do |cart|
cart.marked_for_redeem.should eq(0)
end
end
end
describe "create" do
it "successful" do
email = 'user_create1@jamkazam.com'
post :create, first_name: 'Seth', last_name: 'Call', email: email, password: 'jam123', terms: true, format: 'json'
response.should be_success
found = User.find_by_email(email)
found.city.should be_nil
found.state.should be_nil
found.country.should be_nil
found.musician.should be true
found.musician_instruments.count.should be(1)
found.last_jam_updated_reason.should eq('r')
found.last_jam_locidispid.should_not be_nil
end
it "no first name is OK" do
email = 'user_create2@jamkazam.com'
post :create, first_name: nil, last_name: 'Call', email: email, password: 'jam123', terms: true, format: 'json'
response.status.should eq(201)
end
it "no email" do
email = nil
post :create, first_name: nil, last_name: 'Call', email: email, password: 'jam123', terms: true, format: 'json'
response.status.should eq(422)
error_data = JSON.parse(response.body)
error_data['errors']['email'].should eq(["can't be blank", "is invalid"])
end
it "short password" do
email = nil
post :create, first_name: nil, last_name: 'Call', email: email, password: 'jam', terms: true, format: 'json'
response.status.should eq(422)
error_data = JSON.parse(response.body)
error_data['errors']['password'].should eq(["is too short (minimum is 6 characters)"])
end
it "no terms" do
email = 'user_create3@jamkazam.com'
post :create, first_name: 'Seth', last_name: 'Call', email: email, password: 'jam123', terms: false, format: 'json'
response.status.should eq(422)
error_data = JSON.parse(response.body)
error_data['errors']['terms_of_service'].should eq(["must be accepted"])
end
end
describe "calendars" do
before :each do
Calendar.destroy_all
end
it "adds calendar via update" do
cals = [{
:name=>"Test Cal",
:description=>"This is a test",
:start_at=>(Time.now),
:end_at=>Time.now,
:trigger_delete=>true,
:target_uid=>"2112"
}]
post :update, id:user.id, calendars: cals, :format=>'json'
response.should be_success
user.reload
user.calendars.should have(1).items
end
end
describe "update mod" do
it "empty mod" do
post :update, id:user.id, mods: {}, :format=>'json'
response.should be_success
user.reload
user.mods_json.should == {}
end
it "no_show mod" do
user_id = user.id
mods = {"no_show" => {"something1" => true}}
post :update, id:user.id, mods: mods, :format=>'json'
response.should be_success
# verify that the user object has the mods data
user_again = User.find(user_id)
user_again.mods_json.should == mods
# verify that the response shows the mods structure
json = JSON.parse(response.body)
json["mods"].should == mods
end
end
describe 'site validation' do
it 'checks valid and invalid site types' do
site_types = Utils::SITE_TYPES.clone << 'bandcamp-fan'
site_types.each do |sitetype|
rec_id = nil
case sitetype
when 'url'
valid, invalid = 'http://jamkazam.com', 'http://jamkazamxxx.com'
when 'youtube'
valid, invalid = 'jonathankolyer', 'jonathankolyerxxx'
when 'facebook'
valid, invalid = 'jamkazam', 'jamkazamxxxx'
when 'twitter'
valid, invalid = 'jamkazam', 'jamkazamxxxx'
when 'soundcloud'
valid, invalid = 'zedisdead', 'zedisdeadxxxx'
when 'reverbnation'
valid, invalid = 'jessicabrown', 'jessicabrownasdf'
when 'bandcamp'
valid, invalid = 'hucci', 'huccixxxxxx'
when 'bandcamp-fan'
valid, invalid = 'iguanaden', 'iguanadenxxxxxx'
when 'fandalism'
valid, invalid = 'pud', 'pudxxxx'
when 'rec_youtube'
valid, invalid = 'https://www.youtube.com/watch?v=i_xFOmYxKYw', 'https://www.zzzyoutube.com'
rec_id = 'i_xFOmYxKYw'
when 'rec_soundcloud'
valid, invalid = 'https://soundcloud.com/zedsdead/winter-mix', 'https://soundcloud.com/zedsdead/winter-mixxx'
rec_id = '187320651'
else
valid, invalid = '', ''
end
next if valid.blank?
2.times do |nn|
get :validate_data, sitetype:sitetype, data: 0==nn ? valid : invalid
response.should be_success
json = JSON.parse(response.body)
if Utils.recording_source?(sitetype)
expect(json['message']).to eq(0==nn ? 'Valid Site' : 'Invalid Site')
expect(json['recording_id']).to eq(0==nn ? rec_id : nil)
else
expect(json['message']).to eq(0==nn ? 'Valid Site' : 'Invalid Site')
end
end
end
end
end
describe "audio_latency" do
it "updates both connection and user" do
post :audio_latency, id: user.id, client_id: conn.client_id, audio_latency: 3.5, :format => 'json'
response.should be_success
conn.reload
conn.last_jam_audio_latency.should == 3.5
user.reload
conn.user.last_jam_audio_latency.should == 3.5
end
it "if connection does not exist, user is still updated" do
post :audio_latency, id: user.id, client_id: 'nothingness', audio_latency: 3.5, :format => 'json'
response.should be_success
user.reload
conn.user.last_jam_audio_latency.should == 3.5
end
it "ignores latencies of 2 or less" do
post :audio_latency, id: user.id, client_id: conn.client_id, audio_latency: 2, :format => 'json'
response.should be_success
conn.reload
conn.last_jam_audio_latency.should == 5
user.reload
conn.user.last_jam_audio_latency.should == 5
end
end
describe "get_latencies" do
let(:user1) { FactoryGirl.create(:user) }
let(:user2) { FactoryGirl.create(:user) }
let(:latency_data_uri) { /\S+\/dev\/user_latencies/ }
let(:response_body) { File.open('./spec/fixtures/latency_response.json') }
it "fetch latency graph data" do
stub_request(:post, latency_data_uri)
.to_return( body: response_body, status: 200)
get :get_latencies, id: user.id, user_ids: [user1, user2].map(&:id).join(',')
response.should be_success
JSON.parse(response.body)['users'].size.should eq(2)
end
end
describe "crash_dump" do
include UsesTempFiles
CRASH_TEMP_FILE='crash.txt'
in_directory_with_file(CRASH_TEMP_FILE)
def put_file_to_aws(url, contents)
begin
RestClient.put( url, contents, {content_type: nil})
rescue => e
puts e.response
raise e
end
end
before(:all) do
Rails.application.config.storage_type = :fog
end
it "307 Temporary Redirect to s3" do
content_for_file("this is a crash file")
put :crash_dump, client_type: 'MacOSX', version: '1.0', client_version: '1.0', client_id: '1', session_id: '1', timestamp: 1618246569, fsize: '10K', description: "memory_limit_exceeded", crash_context: "Blahblahblab"
expect(response).to have_http_status(307)
put_file_to_aws(response.location, File.read(CRASH_TEMP_FILE))
end
end
describe "user_assets" do
before(:each) do
UserAsset.destroy_all
end
describe "POST" do
it "returns s3 write url", focus: true do
expect {
post "user_assets", filename: "my_image.jpg", asset_type: 'image', format: 'json'
}.to change(UserAsset, :count).by(1)
expect(response).to have_http_status(200)
expect(response.body).to eq({ id: UserAsset.first.id, url: UserAsset.first.write_url}.to_json)
end
it "fails without required params" do
post "user_assets", filename: "my_image.jpg", format: 'json'
expect(response).to have_http_status(422)
end
end
describe "GET" do
let(:user_asset) { FactoryGirl.create(:user_asset, user_id: user.id, asset_type: 'video', recording_id: 1000, session_id: 2000, ext_id: 3000) }
it "get user_asset by id" do
get :user_assets, id: user_asset.id, format: 'json'
expect(response).to have_http_status(307)
expect(response.location).to eq(user_asset.read_url)
end
it "get user_asset by ext_id" do
get :user_assets, ext_id: user_asset.ext_id, format: 'json'
expect(response).to have_http_status(307)
expect(response.location).to eq(user_asset.read_url)
end
it "get user_asset by asset_type and recording_id" do
get :user_assets, asset_type: user_asset.asset_type, recording_id: user_asset.recording_id, format: 'json'
expect(response).to have_http_status(307)
expect(response.location).to eq(user_asset.read_url)
end
it "get user_asset by asset_type and session_id" do
get :user_assets, asset_type: user_asset.asset_type, session_id: user_asset.session_id, format: 'json'
expect(response).to have_http_status(307)
expect(response.location).to eq(user_asset.read_url)
end
it "returns 404 not_found for invalid id" do
get :user_assets, id: 100, format: 'json'
expect(response).to have_http_status(404)
end
it "returns 404 not_found for invalid ext_id" do
get :user_assets, ext_id: 100, format: 'json'
expect(response).to have_http_status(404)
end
it "returns 404 not_found for invalid asset_type+recording_id" do
get :user_assets, asset_type: user_asset.asset_type, recording_id: 0 , format: 'json'
expect(response).to have_http_status(404)
end
it "returns 404 not_found for invalid asset_type+session_id" do
get :user_assets, asset_type: user_asset.asset_type, session_id: 0 , format: 'json'
expect(response).to have_http_status(404)
end
it "returns message for unsupported params" do
get :user_assets, external_id: 0 , format: 'json'
expect(response).to have_http_status(415)
expect(response.body).to eq("Unsupported query")
end
end
end
end