notes_spec.rb 3.45 KB
Newer Older
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
require 'spec_helper'

describe Gitlab::API do
  include ApiHelpers

  let(:user) { create(:user) }
  let!(:project) { create(:project, owner: user) }
  let!(:issue) { create(:issue, project: project, author: user) }
  let!(:snippet) { create(:snippet, project: project, author: user) }
  let!(:issue_note) { create(:note, noteable: issue, project: project, author: user) }
  let!(:snippet_note) { create(:note, noteable: snippet, project: project, author: user) }
  let!(:wall_note) { create(:note, project: project, author: user) }
  before { project.add_access(user, :read) }

  describe "GET /projects/:id/notes" do
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/projects/#{project.id}/notes")
        response.status.should == 401
      end
    end

    context "when authenticated" do
      it "should return project wall notes" do
        get api("/projects/#{project.id}/notes", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.first['body'].should == wall_note.note
      end
    end
  end

33 34 35 36 37 38 39 40
  describe "POST /projects/:id/notes" do
    it "should create a new wall note" do
      post api("/projects/#{project.id}/notes", user), body: 'hi!'
      response.status.should == 201
      json_response['body'].should == 'hi!'
    end
  end

41 42
  describe "GET /projects/:id/noteable/:noteable_id/notes" do
    context "when noteable is an Issue" do
Nihad Abbasov's avatar
Nihad Abbasov committed
43
      it "should return an array of issue notes" do
44 45 46 47 48 49 50 51
        get api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.first['body'].should == issue_note.note
      end
    end

    context "when noteable is a Snippet" do
Nihad Abbasov's avatar
Nihad Abbasov committed
52
      it "should return an array of snippet notes" do
53 54 55 56 57 58 59
        get api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.first['body'].should == snippet_note.note
      end
    end
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  describe "GET /projects/:id/noteable/:noteable_id/notes/:note_id" do
    context "when noteable is an Issue" do
      it "should return an issue note by id" do
        get api("/projects/#{project.id}/issues/#{issue.id}/notes/#{issue_note.id}", user)
        response.status.should == 200
        json_response['body'].should == issue_note.note
      end
    end

    context "when noteable is a Snippet" do
      it "should return a snippet note by id" do
        get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/#{snippet_note.id}", user)
        response.status.should == 200
        json_response['body'].should == snippet_note.note
      end
    end
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

  describe "POST /projects/:id/noteable/:noteable_id/notes" do
    context "when noteable is an Issue" do
      it "should create a new issue note" do
        post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
        response.status.should == 201
        json_response['body'].should == 'hi!'
        json_response['author']['email'].should == user.email
      end
    end

    context "when noteable is a Snippet" do
      it "should create a new snippet note" do
        post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user), body: 'hi!'
        response.status.should == 201
        json_response['body'].should == 'hi!'
        json_response['author']['email'].should == user.email
      end
    end
  end
98
end