From b2964a64415173b3c089fe3bfd8edbb09e1a20b0 Mon Sep 17 00:00:00 2001 From: Callum Dryden Date: Tue, 17 Jun 2014 21:59:42 +0100 Subject: [PATCH] Limit the show action to just the problem --- app/controllers/api/v1/problems_controller.rb | 3 +-- spec/controllers/api/v1/problems_controller_spec.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v1/problems_controller.rb b/app/controllers/api/v1/problems_controller.rb index 44df483..8dd657c 100644 --- a/app/controllers/api/v1/problems_controller.rb +++ b/app/controllers/api/v1/problems_controller.rb @@ -5,8 +5,7 @@ class Api::V1::ProblemsController < ApplicationController def show result = benchmark("[api/v1/problems_controller/show] query time") do begin - problem = Problem.only(FIELDS).find(params[:id]) - problem.as_json(include: {errs: { include: :notices}}) + Problem.only(FIELDS).find(params[:id]) rescue Mongoid::Errors::DocumentNotFound head :not_found return false diff --git a/spec/controllers/api/v1/problems_controller_spec.rb b/spec/controllers/api/v1/problems_controller_spec.rb index 1be51d7..0f31089 100644 --- a/spec/controllers/api/v1/problems_controller_spec.rb +++ b/spec/controllers/api/v1/problems_controller_spec.rb @@ -11,18 +11,58 @@ describe Api::V1::ProblemsController do before do notice = Fabricate(:notice) err = Fabricate(:err, :notices => [notice]) - problem = Fabricate(:problem, :errs => [err]) + @problem = Fabricate(:problem, :errs => [err]) end - it "returns JSON if JSON is requested" do + it "should return JSON if JSON is requested" do get :show, :auth_token => @user.authentication_token, :format => "json", :id => Problem.first.id + expect { JSON.load(response.body) }.not_to raise_error() #JSON::ParserError + end + + it "should return XML if XML is requested" do + get :index, :auth_token => @user.authentication_token, :format => "xml", :id => @problem.id + expect(Nokogiri::XML(response.body).errors).to be_empty + end + + it "should return JSON by default" do + get :show, :auth_token => @user.authentication_token, :id => @problem.id expect { JSON.load(response.body) }.not_to raise_error()#JSON::ParserError) end - it "returns the correct problem" do - requested_problem = Problem.first - get :show, :auth_token => @user.authentication_token, :format => "json", :id => requested_problem.id - expect( response.body ).to eq(requested_problem.to_json(include: {errs: { include: :notices}})) + it "should return the correct problem" do + get :show, :auth_token => @user.authentication_token, :format => "json", :id => @problem.id + + returned_problem = JSON.parse(response.body) + expect( returned_problem["_id"] ).to eq(@problem.id.to_s) + end + + it "should return only the correct fields" do + get :show, :auth_token => @user.authentication_token, :format => "json", :id => @problem.id + returned_problem = JSON.parse(response.body) + + expect( returned_problem.keys ).to match_array([ + "app_name", + "first_notice_at", + "error_class", + "messages", + "hosts", + "created_at", + "app_id", + "last_notice_at", + "_id", + "issue_link", + "resolved", + "updated_at", + "resolved_at", + "last_deploy_at", + "where", + "issue_type", + "notices_count", + "user_agents", + "comments_count", + "message", + "environment" + ]) end it "returns a 404 if the problem cannot be found" do -- libgit2 0.21.2