BigW Consortium Gitlab

notes_controller_spec.rb 4.82 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 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
require 'spec_helper'

describe Snippets::NotesController do
  let(:user) { create(:user) }

  let(:private_snippet)  { create(:personal_snippet, :private) }
  let(:internal_snippet) { create(:personal_snippet, :internal) }
  let(:public_snippet)   { create(:personal_snippet, :public) }

  let(:note_on_private)  { create(:note_on_personal_snippet, noteable: private_snippet) }
  let(:note_on_internal) { create(:note_on_personal_snippet, noteable: internal_snippet) }
  let(:note_on_public)   { create(:note_on_personal_snippet, noteable: public_snippet) }

  describe 'GET index' do
    context 'when a snippet is public' do
      before do
        note_on_public

        get :index, { snippet_id: public_snippet }
      end

      it "returns status 200" do
        expect(response).to have_http_status(200)
      end

      it "returns not empty array of notes" do
        expect(JSON.parse(response.body)["notes"].empty?).to be_falsey
      end
    end

    context 'when a snippet is internal' do
      before do
        note_on_internal
      end

      context 'when user not logged in' do
        it "returns status 404" do
          get :index, { snippet_id: internal_snippet }

          expect(response).to have_http_status(404)
        end
      end

      context 'when user logged in' do
        before do
          sign_in(user)
        end

        it "returns status 200" do
          get :index, { snippet_id: internal_snippet }

          expect(response).to have_http_status(200)
        end
      end
    end

    context 'when a snippet is private' do
      before do
        note_on_private
      end

      context 'when user not logged in' do
        it "returns status 404" do
          get :index, { snippet_id: private_snippet }

          expect(response).to have_http_status(404)
        end
      end

      context 'when user other than author logged in' do
        before do
          sign_in(user)
        end

        it "returns status 404" do
          get :index, { snippet_id: private_snippet }

          expect(response).to have_http_status(404)
        end
      end

      context 'when author logged in' do
        before do
          note_on_private

          sign_in(private_snippet.author)
        end

        it "returns status 200" do
          get :index, { snippet_id: private_snippet }

          expect(response).to have_http_status(200)
        end

        it "returns 1 note" do
          get :index, { snippet_id: private_snippet }

          expect(JSON.parse(response.body)['notes'].count).to eq(1)
        end
      end
    end

    context 'dont show non visible notes' do
      before do
        note_on_public

        sign_in(user)

        expect_any_instance_of(Note).to receive(:cross_reference_not_visible_for?).and_return(true)
      end

      it "does not return any note" do
        get :index, { snippet_id: public_snippet }

        expect(JSON.parse(response.body)['notes'].count).to eq(0)
      end
    end
  end

  describe 'DELETE destroy' do
    let(:request_params) do
      {
        snippet_id: public_snippet,
        id: note_on_public,
        format: :js
      }
    end

    context 'when user is the author of a note' do
      before do
        sign_in(note_on_public.author)
      end

      it "returns status 200" do
        delete :destroy, request_params

        expect(response).to have_http_status(200)
      end

      it "deletes the note" do
        expect{ delete :destroy, request_params }.to change{ Note.count }.from(1).to(0)
      end

      context 'system note' do
        before do
          expect_any_instance_of(Note).to receive(:system?).and_return(true)
        end

        it "does not delete the note" do
          expect{ delete :destroy, request_params }.not_to change{ Note.count }
        end
      end
    end

    context 'when user is not the author of a note' do
      before do
        sign_in(user)

        note_on_public
      end

      it "returns status 404" do
        delete :destroy, request_params

        expect(response).to have_http_status(404)
      end

      it "does not update the note" do
        expect{ delete :destroy, request_params }.not_to change{ Note.count }
      end
    end
  end

  describe 'POST toggle_award_emoji' do
    let(:note) { create(:note_on_personal_snippet, noteable: public_snippet) }
    before do
      sign_in(user)
    end

    subject { post(:toggle_award_emoji, snippet_id: public_snippet, id: note.id, name: "thumbsup") }

    it "toggles the award emoji" do
      expect { subject }.to change { note.award_emoji.count }.by(1)

      expect(response).to have_http_status(200)
    end

    it "removes the already awarded emoji when it exists" do
      note.toggle_award_emoji('thumbsup', user) # create award emoji before

      expect { subject }.to change { AwardEmoji.count }.by(-1)

      expect(response).to have_http_status(200)
    end
  end
end