mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 02:04:57 +00:00
Add API for App Index
- Add tests for unauthorised response for APIs which require authentication
This commit is contained in:
7
app/controllers/apps_controller.rb
Normal file
7
app/controllers/apps_controller.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
class AppsController < ApplicationController
|
||||||
|
before_action :authenticate_device
|
||||||
|
|
||||||
|
def index
|
||||||
|
render json: App.all
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,11 +2,6 @@ class InstallationsController < ApplicationController
|
|||||||
before_action :authenticate_device
|
before_action :authenticate_device
|
||||||
respond_to :json
|
respond_to :json
|
||||||
|
|
||||||
def downloaded
|
|
||||||
Installation.find(params[:id]).downloaded!
|
|
||||||
render json:{}, status: :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
def installed
|
def installed
|
||||||
Installation.find(params[:id]).installed!
|
Installation.find(params[:id]).installed!
|
||||||
render json:{}, status: :ok
|
render json:{}, status: :ok
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Rails.application.routes.draw do
|
|||||||
post 'heartbeats', to: 'heartbeats#create', :defaults => { :format => :json }
|
post 'heartbeats', to: 'heartbeats#create', :defaults => { :format => :json }
|
||||||
post '/installations/downloaded', :defaults => { :format => :json }
|
post '/installations/downloaded', :defaults => { :format => :json }
|
||||||
post '/installations/installed', :defaults => { :format => :json }
|
post '/installations/installed', :defaults => { :format => :json }
|
||||||
|
get '/apps',to: 'apps#index', :defaults => { :format => :json }
|
||||||
ActiveAdmin.routes(self)
|
ActiveAdmin.routes(self)
|
||||||
# The priority is based upon order of creation: first created -> highest priority.
|
# The priority is based upon order of creation: first created -> highest priority.
|
||||||
# See how all your routes lay out with "rake routes".
|
# See how all your routes lay out with "rake routes".
|
||||||
|
|||||||
26
spec/controllers/apps_controller_spec.rb
Normal file
26
spec/controllers/apps_controller_spec.rb
Normal file
@@ -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
|
||||||
|
|
||||||
|
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe HeartbeatsController, type: :controller do
|
RSpec.describe HeartbeatsController, type: :controller do
|
||||||
let(:heartbeat) {FactoryGirl.create(:heartbeat)}
|
|
||||||
|
|
||||||
before(:each) do
|
context "POST #create" do
|
||||||
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(heartbeat.device.access_token)
|
let(:heartbeat) {FactoryGirl.create(:heartbeat)}
|
||||||
end
|
|
||||||
|
before(:each) do
|
||||||
describe "POST #create" do
|
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(heartbeat.device.access_token)
|
||||||
|
end
|
||||||
|
|
||||||
it "Respond with next heartbeat time" do
|
it "Respond with next heartbeat time" do
|
||||||
post :create, format: :json
|
post :create, format: :json
|
||||||
expect(response).to have_http_status(:created)
|
expect(response).to have_http_status(:created)
|
||||||
@@ -15,5 +16,12 @@ RSpec.describe HeartbeatsController, type: :controller do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "Require Authentication" do
|
||||||
|
it "#create" do
|
||||||
|
post :create, format: :json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,25 +3,22 @@ require 'rails_helper'
|
|||||||
RSpec.describe InstallationsController, type: :controller do
|
RSpec.describe InstallationsController, type: :controller do
|
||||||
let(:installation) {FactoryGirl.create(:installation)}
|
let(:installation) {FactoryGirl.create(:installation)}
|
||||||
let(:device){installation.device}
|
let(:device){installation.device}
|
||||||
before(:each)do
|
context "With Authentication" do
|
||||||
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token)
|
before(:each)do
|
||||||
end
|
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token)
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe "POST #installed" do
|
|
||||||
|
|
||||||
it "update installation with installed status" do
|
it "POST #installed" do
|
||||||
post :installed, :id => installation.id , format: :json
|
post :installed, :id => installation.id , format: :json
|
||||||
expect(response).to have_http_status(:ok)
|
expect(response).to have_http_status(:ok)
|
||||||
expect(Installation.last.installed?).to be true
|
expect(Installation.last.installed?).to be true
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user