From a5500f1377e65ada8586832ece8e132f94bfbe4f Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 13 Apr 2016 19:53:03 -0400 Subject: [PATCH 1/4] Adding SimpleCov code coverage testing. --- Gemfile | 1 + Gemfile.lock | 9 ++++++++- spec/rails_helper.rb | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 9876892..af84ceb 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ group :development, :test do gem 'spring' gem 'rspec-rails' gem "factory_girl_rails", "~> 4.0" + gem 'simplecov', '~> 0.11' end # To use ActiveModel has_secure_password diff --git a/Gemfile.lock b/Gemfile.lock index ba4548d..44d259a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,6 +40,7 @@ GEM builder (3.2.2) coderay (1.1.0) diff-lcs (1.2.5) + docile (1.1.5) erubis (2.7.0) factory_girl (4.5.0) activesupport (>= 3.0.0) @@ -113,6 +114,11 @@ GEM rspec-mocks (~> 3.3.0) rspec-support (~> 3.3.0) rspec-support (3.3.0) + simplecov (0.11.2) + docile (~> 1.1.0) + json (~> 1.8) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.0) slop (3.6.0) spring (1.3.6) sprockets (3.3.4) @@ -138,8 +144,9 @@ DEPENDENCIES rack-cors rails (= 4.2.3) rspec-rails + simplecov (~> 0.11) spring sqlite3 BUNDLED WITH - 1.10.6 + 1.11.2 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 01513ae..53187b8 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,3 +1,6 @@ +require 'simplecov' +SimpleCov.start + # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) From 0c2a27421704128798cd36cc006dd6cc6eab128f Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 13 Apr 2016 20:06:22 -0400 Subject: [PATCH 2/4] Adding unauthorized test. --- spec/requests/locations_spec.rb | 6 ++++++ spec/support/request_helper.rb | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/spec/requests/locations_spec.rb b/spec/requests/locations_spec.rb index 8259e7e..25eb328 100644 --- a/spec/requests/locations_spec.rb +++ b/spec/requests/locations_spec.rb @@ -27,6 +27,12 @@ end describe "GET /locations without locations" do + it "fails with unauthorized headers" do + get "/locations", {}, unauthorized_headers + + expect(response.status).to eq 401 + end + it "returns an emtpy data array" do get "/locations", {}, authorized_headers diff --git a/spec/support/request_helper.rb b/spec/support/request_helper.rb index c1262bd..fcb9e30 100644 --- a/spec/support/request_helper.rb +++ b/spec/support/request_helper.rb @@ -13,6 +13,13 @@ def authorized_preflight_headers 'ORIGIN' => 'http://otherdomain.test/' } end + + def unauthorized_headers + { + 'HTTP_ACCEPT' => 'application/vnd.api+json', + 'HTTP_AUTHORIZATION' => 'Token badtokenauth' + } + end end module ActionDispatch::Integration::RequestHelpers From 257c58c786cb87aa27e267f7cae45143e3711dc4 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 13 Apr 2016 20:56:50 -0400 Subject: [PATCH 3/4] Getting most of the routing and controller setup done. --- app/controllers/locations_controller.rb | 6 +++++- app/controllers/properties_controller.rb | 8 ++++++++ app/controllers/things_controller.rb | 8 ++++++++ app/models/location.rb | 1 - app/serializers/thing_serializer.rb | 17 +++++++++++++++++ config/routes.rb | 8 ++++++-- spec/requests/locations_spec.rb | 2 ++ 7 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 app/serializers/thing_serializer.rb diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index ef84d9e..aa16275 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -1,7 +1,11 @@ class LocationsController < ApplicationController def index locations = Location.all - render json: locations end + + def show + location = Location.find(params[:id]) + render json: location + end end diff --git a/app/controllers/properties_controller.rb b/app/controllers/properties_controller.rb index c2c9881..d285b87 100644 --- a/app/controllers/properties_controller.rb +++ b/app/controllers/properties_controller.rb @@ -1,3 +1,11 @@ class PropertiesController < ApplicationController + def index + properties = Property.all + render json: properties + end + def show + property = Property.find(params[:id]) + render json: property + end end diff --git a/app/controllers/things_controller.rb b/app/controllers/things_controller.rb index e7da9e4..4eed1d8 100644 --- a/app/controllers/things_controller.rb +++ b/app/controllers/things_controller.rb @@ -1,3 +1,11 @@ class ThingsController < ApplicationController + def index + things = Thing.all + render json: things + end + def show + thing = Thing.find(params[:id]) + render json: thing + end end diff --git a/app/models/location.rb b/app/models/location.rb index c41b469..b72a4d5 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -2,5 +2,4 @@ class Location < ActiveRecord::Base validates :name, uniqueness: true, presence: true has_many :things - end diff --git a/app/serializers/thing_serializer.rb b/app/serializers/thing_serializer.rb new file mode 100644 index 0000000..d8981e0 --- /dev/null +++ b/app/serializers/thing_serializer.rb @@ -0,0 +1,17 @@ +class ThingSerializer < ActiveModel::Serializer + attributes :name, :status, :reason + + belongs_to :location + + has_many :properties + + def initialize(model, context = nil) + super model, context + model.properties.each do |prop| + self.class.send(:define_method, prop.name.to_sym) do + prop.value + end + self.class.send(:attribute, prop.name.to_sym) + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 007e434..6e1fd0d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,11 @@ # jsonapi_resources :things # jsonapi_resources :properties - resources :locations - resources :things + resources :locations do + resources :things + end + resources :things do + resources :properties + end resources :properties end diff --git a/spec/requests/locations_spec.rb b/spec/requests/locations_spec.rb index 25eb328..b683bc7 100644 --- a/spec/requests/locations_spec.rb +++ b/spec/requests/locations_spec.rb @@ -60,6 +60,8 @@ expect(body['data']).not_to eq nil body['data'].each do |thing| + puts "This is the thing" + puts thing expect(thing['id']).not_to eq nil expect(thing['type']).not_to eq nil expect(thing['links']).not_to eq nil From 06a7fa62c2ee6c4e0b85e4a35e5edf14f9765bde Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 13 Apr 2016 20:59:33 -0400 Subject: [PATCH 4/4] Route for nested things under properties. --- config/routes.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 6e1fd0d..9158eb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,5 +9,7 @@ resources :things do resources :properties end - resources :properties + resources :properties do + resources :things + end end