Commit 657e481e by John Doe

added new specs for home view and view updating

parent b5809eab
......@@ -20,7 +20,9 @@ class DataViewsController < ApplicationController
# POST /data_views.json
def create
@service = CreateDataView.new()
@service.run(data_view_params, params[:stream_ids], current_user)
home_view = params[:home]==true
@service.run(data_view_params, params[:stream_ids],
current_user, home_view)
@data_view = @service.data_view
render :show, status: @service.success? ? :ok : :unprocessable_entity
end
......@@ -29,6 +31,13 @@ class DataViewsController < ApplicationController
def update
@service = StubService.new
if @data_view.update(updatable_data_view_params)
#set the user home view if param[:home] is set
if(params[:home])
current_user.update(home_data_view: @data_view)
#otherwise clear it if this is is the current home view
elsif(current_user.home_data_view==@data_view)
current_user.update(home_data_view: nil)
end
@service.add_notice('updated data view')
render :show, status: :ok
else
......
......@@ -6,14 +6,7 @@ class CreateDataView
attr_reader :data_view
def run(data_view_params, stream_ids, user, home_view=false)
if home_view
@data_view = DataView.create(
data_view_params.merge(
{name: 'home_view', visibility: 'hidden', owner: user}))
user.home_data_view.destroy unless user.home_data_view.nil?
user.update(home_data_view: @data_view)
return self
end
# normal data view
@data_view = DataView.new(data_view_params.merge({owner: user}))
......@@ -36,6 +29,7 @@ class CreateDataView
return self
end
@data_view.nilm_ids = nilm_ids
user.update(home_data_view: @data_view) if home_view
self.set_notice('created data view')
self
end
......
......@@ -2,4 +2,5 @@
json.array! @data_views do |view|
json.extract! view, *DataView.json_keys
json.owner current_user==view.owner
json.home current_user.home_data_view_id == view.id
end
json.data do
json.extract! @data_view, *DataView.json_keys
json.owner current_user==@data_view.owner
json.home current_user.home_data_view_id == @data_view.id
end
json.partial! "helpers/messages", service: @service
......@@ -91,6 +91,21 @@ RSpec.describe DataViewsController, type: :request do
#viewer should own this new dataview
expect(body['data']['owner']).to be(true)
end
it 'creates home data views' do
@auth_headers = viewer.create_new_auth_token
post "/data_views.json",
params: {
home: true,
name: 'new home view', description: '', image: '', redux_json: '',
visibility: 'public', stream_ids: viewed_streams.map {|x| x.id}
}, headers: @auth_headers, as: :json
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
body = JSON.parse(response.body)
#viewer should own this new dataview
expect(body['data']['owner']).to be(true)
expect(viewer.reload.home_data_view.name).to eq('new home view')
end
it 'returns error with bad parameters' do
@auth_headers = viewer.create_new_auth_token
post "/data_views.json",
......@@ -110,6 +125,77 @@ RSpec.describe DataViewsController, type: :request do
end
end
describe 'PUT update' do
context 'with view owner' do
it 'can set home view' do
view = create(:data_view, name: 'old name', owner: viewer)
@auth_headers = viewer.create_new_auth_token
put "/data_views/#{view.id}.json",
params: {
name: 'new name', home: true
}, headers: @auth_headers, as: :json
expect(viewer.reload.home_data_view).to eq(view)
expect(view.reload.name).to eq 'new name'
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
end
it 'can remove home view' do
view = create(:data_view, name: 'old name', owner: viewer)
viewer.update(home_data_view: view)
@auth_headers = viewer.create_new_auth_token
put "/data_views/#{view.id}.json",
params: {
name: 'new name', home: false
}, headers: @auth_headers, as: :json
expect(viewer.reload.home_data_view).to be nil
expect(view.reload.name).to eq 'new name'
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
end
it 'cannot change redux_json or image' do
view = create(:data_view, name: 'old name',
redux_json: 'valid_json', image: 'valid_image', owner: viewer)
viewer.update(home_data_view: view)
@auth_headers = viewer.create_new_auth_token
put "/data_views/#{view.id}.json",
params: {
name: 'new name', redux_json: 'banned', image: 'banned'
}, headers: @auth_headers, as: :json
expect(viewer.reload.home_data_view).to be nil
expect(view.reload.name).to eq 'new name'
expect(view.redux_json).to eq 'valid_json'
expect(view.image).to eq 'valid_image'
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
end
end
context 'with anybody else' do
it 'returns unauthorized' do
other_user = create(:user)
view = create(:data_view, name: 'old name', owner: viewer)
@auth_headers = other_user.create_new_auth_token
put "/data_views/#{view.id}.json",
params: {
name: 'new name'
}, headers: @auth_headers, as: :json
expect(view.reload.name).to eq 'old name'
expect(response).to have_http_status(:unauthorized)
end
end
context 'without sign-in' do
it 'returns unauthorized' do
view = create(:data_view, name: 'old name', owner: viewer)
put "/data_views/#{view.id}.json",
params: {
name: 'new name'
}, as: :json
expect(view.reload.name).to eq 'old name'
expect(response).to have_http_status(:unauthorized)
end
end
end
describe 'DELETE destroy' do
before do
service = CreateDataView.new
......
......@@ -30,18 +30,10 @@ describe 'CreateDataView service' do
end
it 'creates user home views' do
#new home view replaces the previous one
viewer.update(home_data_view: create(:data_view))
expect(DataView.count).to eq(1)
#now add the new one
@service.run(@params, @stream_ids, viewer, home_view=true)
expect(@service.success?).to be true
#previous view should be deleted
expect(DataView.count).to eq(1)
view = DataView.first
expect(view.visibility).to eq 'hidden'
expect(viewer.home_data_view).to eq(view)
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment