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

434 lines
14 KiB
Ruby
Raw Permalink Normal View History

require 'spec_helper'
require 'webmock/rspec'
describe ApiUsersController, type: :controller do
render_views
let (:user) { FactoryGirl.create(:user) }
2014-08-18 15:37:55 +00:00
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])
2015-11-29 19:58:10 +00:00
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])
2015-11-29 19:58:10 +00:00
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])
2015-11-29 19:58:10 +00:00
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])
2015-11-29 19:58:10 +00:00
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])
2015-11-29 19:58:10 +00:00
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
2016-08-03 01:46:15 +00:00
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'
2016-08-03 01:46:15 +00:00
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
Merge feature/calendaring branch: commit 8023d6481cbadd52e58b9a4342ac7636ce1807e3 VRFS-3276 : Hook calendar creation into user controller API. Add test to verify. commit 3a35002a46f870e2c490b88b3187e0b1569494fd VRFS-3276 : Calendar cleanup job * Add cleanup method to calendar manager * Create a daily job. * Add calendar cleanup to that job. * Add CRON entry * Daily job/ calendar cleanup test cases * Fix calendar manager spec for new required attribute commit 3ff5910f1f019ae8bcb5afe72a31f1d38bb7d7a3 VRFS-3276 : Add a delete-calendar directive when RSVP is canceled. VRFS-3276 : Include path to partial. This fails depending on the method used to start the web server. commit d2441cbf57e50895ac3b40534873c5d529cb3c4f VRFS-3276 : Test new calendar features. Use icalendar gem in test mode only to more deeply verify calendar in strict mode. commit 9ac272a0fb1e58d8cf9f02e7a0e04caada41f659 VRFS-3276 : Calendar manager updates to include manual calendars. Some refactoring to keep common stuff in one place. commit b5d0c758f0dcae41a5f24635e9da9ce6eda56670 VRFS-3276 : Schema, model updates and new calendar model. commit 20472b6b26c88c04edb9bc698e0c06c549e12eb5 VRFS-3276 : Change initial submit behavior of RSVP dialog to display calendar info. The user can then close the dialog after this prompt. commit 77c99103d0221f20ea342169821b90fa987ecf93 VRFS-3276 : Calendar feed markup and styling. Included as partial. commit e632f48600ae23b5f742773310b2a4ac16ae4ee8 VRFS-3276 : Routes and controller implementation of user calendar ICS feed, which uses calendar manager. commit 21fd80a188eae771a65333566b804ade795a1e8c VRFS-3276 : Initial tests for calendar manager commit 92a2524c65abf7b540f9d50049a1b760a5a9927f VRFS-3276 : Calendar manager * Streamline logic * Enable recurring sessions through rrule * Implement method to create ics feed for user * Extract a type-safe scheduled duration method on music_session for external and internal use. commit b71ad3a4cdd943eb84748abaa85fec263b9af468 VRFS-3276 : Include calendar manager commit f8eaafd03647613dafec9f9422282f8613d08e9a VRFS-3276 : Calendar Manager - initial checkin * Create ICS events given individual parameters * Create calendar from music session * Also will create ICS “delete” events
2015-07-06 20:34:27 +00:00
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
Merge feature/calendaring branch: commit 8023d6481cbadd52e58b9a4342ac7636ce1807e3 VRFS-3276 : Hook calendar creation into user controller API. Add test to verify. commit 3a35002a46f870e2c490b88b3187e0b1569494fd VRFS-3276 : Calendar cleanup job * Add cleanup method to calendar manager * Create a daily job. * Add calendar cleanup to that job. * Add CRON entry * Daily job/ calendar cleanup test cases * Fix calendar manager spec for new required attribute commit 3ff5910f1f019ae8bcb5afe72a31f1d38bb7d7a3 VRFS-3276 : Add a delete-calendar directive when RSVP is canceled. VRFS-3276 : Include path to partial. This fails depending on the method used to start the web server. commit d2441cbf57e50895ac3b40534873c5d529cb3c4f VRFS-3276 : Test new calendar features. Use icalendar gem in test mode only to more deeply verify calendar in strict mode. commit 9ac272a0fb1e58d8cf9f02e7a0e04caada41f659 VRFS-3276 : Calendar manager updates to include manual calendars. Some refactoring to keep common stuff in one place. commit b5d0c758f0dcae41a5f24635e9da9ce6eda56670 VRFS-3276 : Schema, model updates and new calendar model. commit 20472b6b26c88c04edb9bc698e0c06c549e12eb5 VRFS-3276 : Change initial submit behavior of RSVP dialog to display calendar info. The user can then close the dialog after this prompt. commit 77c99103d0221f20ea342169821b90fa987ecf93 VRFS-3276 : Calendar feed markup and styling. Included as partial. commit e632f48600ae23b5f742773310b2a4ac16ae4ee8 VRFS-3276 : Routes and controller implementation of user calendar ICS feed, which uses calendar manager. commit 21fd80a188eae771a65333566b804ade795a1e8c VRFS-3276 : Initial tests for calendar manager commit 92a2524c65abf7b540f9d50049a1b760a5a9927f VRFS-3276 : Calendar manager * Streamline logic * Enable recurring sessions through rrule * Implement method to create ics feed for user * Extract a type-safe scheduled duration method on music_session for external and internal use. commit b71ad3a4cdd943eb84748abaa85fec263b9af468 VRFS-3276 : Include calendar manager commit f8eaafd03647613dafec9f9422282f8613d08e9a VRFS-3276 : Calendar Manager - initial checkin * Create ICS events given individual parameters * Create calendar from music session * Also will create ICS “delete” events
2015-07-06 20:34:27 +00:00
describe 'site validation' do
Merge feature/calendaring branch: commit 8023d6481cbadd52e58b9a4342ac7636ce1807e3 VRFS-3276 : Hook calendar creation into user controller API. Add test to verify. commit 3a35002a46f870e2c490b88b3187e0b1569494fd VRFS-3276 : Calendar cleanup job * Add cleanup method to calendar manager * Create a daily job. * Add calendar cleanup to that job. * Add CRON entry * Daily job/ calendar cleanup test cases * Fix calendar manager spec for new required attribute commit 3ff5910f1f019ae8bcb5afe72a31f1d38bb7d7a3 VRFS-3276 : Add a delete-calendar directive when RSVP is canceled. VRFS-3276 : Include path to partial. This fails depending on the method used to start the web server. commit d2441cbf57e50895ac3b40534873c5d529cb3c4f VRFS-3276 : Test new calendar features. Use icalendar gem in test mode only to more deeply verify calendar in strict mode. commit 9ac272a0fb1e58d8cf9f02e7a0e04caada41f659 VRFS-3276 : Calendar manager updates to include manual calendars. Some refactoring to keep common stuff in one place. commit b5d0c758f0dcae41a5f24635e9da9ce6eda56670 VRFS-3276 : Schema, model updates and new calendar model. commit 20472b6b26c88c04edb9bc698e0c06c549e12eb5 VRFS-3276 : Change initial submit behavior of RSVP dialog to display calendar info. The user can then close the dialog after this prompt. commit 77c99103d0221f20ea342169821b90fa987ecf93 VRFS-3276 : Calendar feed markup and styling. Included as partial. commit e632f48600ae23b5f742773310b2a4ac16ae4ee8 VRFS-3276 : Routes and controller implementation of user calendar ICS feed, which uses calendar manager. commit 21fd80a188eae771a65333566b804ade795a1e8c VRFS-3276 : Initial tests for calendar manager commit 92a2524c65abf7b540f9d50049a1b760a5a9927f VRFS-3276 : Calendar manager * Streamline logic * Enable recurring sessions through rrule * Implement method to create ics feed for user * Extract a type-safe scheduled duration method on music_session for external and internal use. commit b71ad3a4cdd943eb84748abaa85fec263b9af468 VRFS-3276 : Include calendar manager commit f8eaafd03647613dafec9f9422282f8613d08e9a VRFS-3276 : Calendar Manager - initial checkin * Create ICS events given individual parameters * Create calendar from music session * Also will create ICS “delete” events
2015-07-06 20:34:27 +00:00
it 'checks valid and invalid site types' do
site_types = Utils::SITE_TYPES.clone << 'bandcamp-fan'
site_types.each do |sitetype|
rec_id = nil
Merge feature/calendaring branch: commit 8023d6481cbadd52e58b9a4342ac7636ce1807e3 VRFS-3276 : Hook calendar creation into user controller API. Add test to verify. commit 3a35002a46f870e2c490b88b3187e0b1569494fd VRFS-3276 : Calendar cleanup job * Add cleanup method to calendar manager * Create a daily job. * Add calendar cleanup to that job. * Add CRON entry * Daily job/ calendar cleanup test cases * Fix calendar manager spec for new required attribute commit 3ff5910f1f019ae8bcb5afe72a31f1d38bb7d7a3 VRFS-3276 : Add a delete-calendar directive when RSVP is canceled. VRFS-3276 : Include path to partial. This fails depending on the method used to start the web server. commit d2441cbf57e50895ac3b40534873c5d529cb3c4f VRFS-3276 : Test new calendar features. Use icalendar gem in test mode only to more deeply verify calendar in strict mode. commit 9ac272a0fb1e58d8cf9f02e7a0e04caada41f659 VRFS-3276 : Calendar manager updates to include manual calendars. Some refactoring to keep common stuff in one place. commit b5d0c758f0dcae41a5f24635e9da9ce6eda56670 VRFS-3276 : Schema, model updates and new calendar model. commit 20472b6b26c88c04edb9bc698e0c06c549e12eb5 VRFS-3276 : Change initial submit behavior of RSVP dialog to display calendar info. The user can then close the dialog after this prompt. commit 77c99103d0221f20ea342169821b90fa987ecf93 VRFS-3276 : Calendar feed markup and styling. Included as partial. commit e632f48600ae23b5f742773310b2a4ac16ae4ee8 VRFS-3276 : Routes and controller implementation of user calendar ICS feed, which uses calendar manager. commit 21fd80a188eae771a65333566b804ade795a1e8c VRFS-3276 : Initial tests for calendar manager commit 92a2524c65abf7b540f9d50049a1b760a5a9927f VRFS-3276 : Calendar manager * Streamline logic * Enable recurring sessions through rrule * Implement method to create ics feed for user * Extract a type-safe scheduled duration method on music_session for external and internal use. commit b71ad3a4cdd943eb84748abaa85fec263b9af468 VRFS-3276 : Include calendar manager commit f8eaafd03647613dafec9f9422282f8613d08e9a VRFS-3276 : Calendar Manager - initial checkin * Create ICS events given individual parameters * Create calendar from music session * Also will create ICS “delete” events
2015-07-06 20:34:27 +00:00
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
2014-08-18 15:37:55 +00:00
post :audio_latency, id: user.id, client_id: conn.client_id, audio_latency: 3.5, :format => 'json'
response.should be_success
conn.reload
2014-08-18 15:37:55 +00:00
conn.last_jam_audio_latency.should == 3.5
user.reload
2014-08-18 15:37:55 +00:00
conn.user.last_jam_audio_latency.should == 3.5
end
it "if connection does not exist, user is still updated" do
2014-08-18 15:37:55 +00:00
post :audio_latency, id: user.id, client_id: 'nothingness', audio_latency: 3.5, :format => 'json'
response.should be_success
user.reload
2014-08-18 15:37:55 +00:00
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
2021-04-12 18:50:46 +00:00
describe "get_latencies" do
let(:user1) { FactoryGirl.create(:user) }
let(:user2) { FactoryGirl.create(:user) }
2021-05-29 09:08:34 +00:00
let(:latency_data_uri) { /\S+\/dev\/user_latencies/ }
let(:response_body) { File.open('./spec/fixtures/latency_response.json') }
it "fetch latency graph data" do
2021-05-29 09:08:34 +00:00
stub_request(:post, latency_data_uri)
.to_return( body: response_body, status: 200)
2021-05-29 09:08:34 +00:00
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
2021-04-12 18:50:46 +00:00
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})
2021-04-12 18:50:46 +00:00
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