From 879a0f49e1ceeb88e6ef361993b2b9ed0dc36d17 Mon Sep 17 00:00:00 2001 From: leenasn Date: Mon, 23 Nov 2015 12:05:45 +0530 Subject: [PATCH] Add API for App Index - Add tests for unauthorised response for APIs which require authentication --- app/controllers/apps_controller.rb | 7 +++++ app/controllers/installations_controller.rb | 5 ---- config/routes.rb | 2 +- spec/controllers/apps_controller_spec.rb | 26 +++++++++++++++++++ .../controllers/heartbeats_controller_spec.rb | 20 +++++++++----- .../installations_controller_spec.rb | 25 ++++++++---------- 6 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 app/controllers/apps_controller.rb create mode 100644 spec/controllers/apps_controller_spec.rb diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb new file mode 100644 index 0000000..77d7484 --- /dev/null +++ b/app/controllers/apps_controller.rb @@ -0,0 +1,7 @@ +class AppsController < ApplicationController + before_action :authenticate_device + + def index + render json: App.all + end +end diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index 65937c3..cb69950 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -2,11 +2,6 @@ class InstallationsController < ApplicationController before_action :authenticate_device respond_to :json - def downloaded - Installation.find(params[:id]).downloaded! - render json:{}, status: :ok - end - def installed Installation.find(params[:id]).installed! render json:{}, status: :ok diff --git a/config/routes.rb b/config/routes.rb index cc71ba6..8203607 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ Rails.application.routes.draw do post 'heartbeats', to: 'heartbeats#create', :defaults => { :format => :json } post '/installations/downloaded', :defaults => { :format => :json } post '/installations/installed', :defaults => { :format => :json } - + get '/apps',to: 'apps#index', :defaults => { :format => :json } ActiveAdmin.routes(self) # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb new file mode 100644 index 0000000..f028f39 --- /dev/null +++ b/spec/controllers/apps_controller_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe AppsController, type: :controller do + context "With Authentication" do + let(:device){create(:device)} + before(:each) do + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token) + end + it "#index" do + expect(App).to receive(:all) + + get :index, format: :json + + expect(response).to have_http_status(:ok) + end + end + context "Without Authentication" do + it "#index" do + get :index, format: :json + + expect(response).to have_http_status(:unauthorized) + end + end +end + + diff --git a/spec/controllers/heartbeats_controller_spec.rb b/spec/controllers/heartbeats_controller_spec.rb index 9a5228f..9fa4fb8 100644 --- a/spec/controllers/heartbeats_controller_spec.rb +++ b/spec/controllers/heartbeats_controller_spec.rb @@ -1,13 +1,14 @@ require 'rails_helper' RSpec.describe HeartbeatsController, type: :controller do - let(:heartbeat) {FactoryGirl.create(:heartbeat)} - before(:each) do - request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(heartbeat.device.access_token) - end - - describe "POST #create" do + context "POST #create" do + let(:heartbeat) {FactoryGirl.create(:heartbeat)} + + before(:each) do + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(heartbeat.device.access_token) + end + it "Respond with next heartbeat time" do post :create, format: :json expect(response).to have_http_status(:created) @@ -15,5 +16,12 @@ RSpec.describe HeartbeatsController, type: :controller do end end + context "Require Authentication" do + it "#create" do + post :create, format: :json + + expect(response).to have_http_status(:unauthorized) + end + end end diff --git a/spec/controllers/installations_controller_spec.rb b/spec/controllers/installations_controller_spec.rb index 3027599..2868234 100644 --- a/spec/controllers/installations_controller_spec.rb +++ b/spec/controllers/installations_controller_spec.rb @@ -3,25 +3,22 @@ require 'rails_helper' RSpec.describe InstallationsController, type: :controller do let(:installation) {FactoryGirl.create(:installation)} let(:device){installation.device} - before(:each)do - request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token) - end - - describe "POST #downloaded" do - - it "upate installation with download status" do - post :downloaded, :id => installation.id , format: :json - expect(response).to have_http_status(:ok) - expect(Installation.last.downloaded?).to be true + context "With Authentication" do + before(:each)do + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token) end - end - - describe "POST #installed" do - it "update installation with installed status" do + it "POST #installed" do post :installed, :id => installation.id , format: :json expect(response).to have_http_status(:ok) expect(Installation.last.installed?).to be true end end + + context "Require Authentication" do + it "POST #installed" do + post :installed, :id => installation.id , format: :json + expect(response).to have_http_status(:unauthorized) + end + end end