Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 143 lines (115 sloc) 3.831 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
require "spec_helper"

describe "/api/v1/projects", :type => :api do
  let(:user) { create_user! }
  let(:token) { user.authentication_token }
  
  before do
    @project = Factory(:project)
    user.permissions.create!(:action => "view", :thing => @project)
  end

  context "projects viewable by this user" do
    before do
      Factory(:project, :name => "Access denied.")
    end

    let(:url) { "/api/v1/projects" }
    it "JSON" do
      get "#{url}.json", :token => token

      projects_json = Project.for(user).all.to_json
      last_response.body.should eql(projects_json)
      last_response.status.should eql(200)

      projects = JSON.parse(last_response.body)

      projects.any? do |p|
        p["project"]["name"] == "Ticketee"
      end.should be_true
      
      projects.any? do |p|
        p["project"]["name"] == "Access Denied"
      end.should be_false
    end
    
    it "XML" do
      get "#{url}.xml", :token => token
      last_response.body.should eql(Project.readable_by(user).to_xml)
      projects = Nokogiri::XML(last_response.body)
      projects.css("project name").text.should eql("Ticketee")
    end
  end
  
  context "creating a project" do
    before do
      user.admin = true
      user.save
    end

    let(:url) { "/api/v1/projects" }

    it "sucessful JSON" do
      post "#{url}.json", :token => token,
                          :project => {
                            :name => "Inspector"
                          }

      project = Project.find_by_name("Inspector")
      route = "/api/v1/projects/#{project.id}"

      last_response.status.should eql(201)
      last_response.headers["Location"].should eql(route)
      
      last_response.body.should eql(project.to_json)
    end
    
    it "unsuccessful JSON" do
      post "#{url}.json", :token => token,
                          :project => {}
      last_response.status.should eql(422)
      errors = {"name" => ["can't be blank"]}.to_json
      last_response.body.should eql(errors)
    end
  end
  
  context "show" do
    let(:url) { "/api/v1/projects/#{@project.id}"}

    before do
      Factory(:ticket, :project => @project)
    end

    it "JSON" do
      get "#{url}.json", :token => token
      project = @project.to_json(:methods => "last_ticket")
      last_response.body.should eql(project)
      last_response.status.should eql(200)

      project_response = JSON.parse(last_response.body)["project"]

      ticket_title = project_response["last_ticket"]["ticket"]["title"]
      ticket_title.should_not be_blank
    end
  end
  
  context "updating a project" do
    before do
      user.admin = true
      user.save
    end

    let(:url) { "/api/v1/projects/#{@project.id}" }
    it "successful JSON" do
      @project.name.should eql("Ticketee")
      put "#{url}.json", :token => token,
                          :project => {
                            :name => "Not Ticketee"
                          }
      last_response.status.should eql(200)

      @project.reload
      @project.name.should eql("Not Ticketee")
      last_response.body.should eql("{}")
    end
    
    it "unsuccessful JSON" do
      @project.name.should eql("Ticketee")
      put "#{url}.json", :token => token,
                          :project => {
                            :name => ""
                          }
      last_response.status.should eql(422)

      @project.reload
      @project.name.should eql("Ticketee")
      errors = { :name => ["can't be blank"]}
      last_response.body.should eql(errors.to_json)
    end
  end
  
  context "deleting a project" do
    before do
      user.admin = true
      user.save
    end

    let(:url) { "/api/v1/projects/#{@project.id}" }
    it "JSON" do
      delete "#{url}.json", :token => token
      last_response.status.should eql(200)
    end
  end
end
Something went wrong with that request. Please try again.