note_spec.rb 5.2 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# == Schema Information
#
# Table name: notes
#
#  id            :integer         not null, primary key
#  note          :text
#  noteable_id   :string(255)
#  noteable_type :string(255)
#  author_id     :integer
#  created_at    :datetime        not null
#  updated_at    :datetime        not null
#  project_id    :integer
#  attachment    :string(255)
#  line_code     :string(255)
#

G
gitlabhq 已提交
17 18 19 20 21
require 'spec_helper'

describe Note do
  describe "Associations" do
    it { should belong_to(:project) }
22 23
    it { should belong_to(:noteable) }
    it { should belong_to(:author).class_name('User') }
G
gitlabhq 已提交
24 25
  end

26 27 28 29 30
  describe "Mass assignment" do
    it { should_not allow_mass_assignment_of(:author) }
    it { should_not allow_mass_assignment_of(:author_id) }
  end

G
gitlabhq 已提交
31 32 33 34 35
  describe "Validation" do
    it { should validate_presence_of(:note) }
    it { should validate_presence_of(:project) }
  end

D
Drew 已提交
36 37 38 39 40
  describe "Scopes" do
    it "should have a today named scope that returns ..." do
      Note.today.where_values.should == ["created_at >= '#{Date.today}'"]
    end
  end
D
Dmitriy Zaporozhets 已提交
41

42
  describe "Voting score" do
43
    let(:project) { create(:project) }
44 45

    it "recognizes a neutral note" do
46
      note = create(:note, note: "This is not a +1 note")
47
      note.should_not be_upvote
48 49 50 51 52 53 54
      note.should_not be_downvote
    end

    it "recognizes a neutral emoji note" do
      note = build(:note, note: "I would :+1: this, but I don't want to")
      note.should_not be_upvote
      note.should_not be_downvote
55 56 57
    end

    it "recognizes a +1 note" do
58
      note = create(:note, note: "+1 for this")
59 60 61
      note.should be_upvote
    end

62 63 64 65 66
    it "recognizes a +1 emoji as a vote" do
      note = build(:note, note: ":+1: for this")
      note.should be_upvote
    end

67
    it "recognizes a -1 note" do
68
      note = create(:note, note: "-1 for this")
69 70 71 72 73 74
      note.should be_downvote
    end

    it "recognizes a -1 emoji as a vote" do
      note = build(:note, note: ":-1: for this")
      note.should be_downvote
75
    end
76 77
  end

78 79
  let(:project) { create(:project) }
  let(:commit) { project.commit }
80

81
  describe "Commit notes" do
82
    before do
83 84 85
      @note = create(:note,
                     noteable_id: commit.id,
                     noteable_type: "Commit")
D
Dmitriy Zaporozhets 已提交
86 87
    end

R
Riyad Preukschas 已提交
88 89 90 91 92 93
    it "should be accessible through #noteable" do
      @note.noteable_id.should == commit.id
      @note.noteable.should be_a(Commit)
      @note.noteable.should == commit
    end

D
Dmitriy Zaporozhets 已提交
94 95
    it "should save a valid note" do
      @note.noteable_id.should == commit.id
R
Riyad Preukschas 已提交
96 97 98 99 100
      @note.noteable == commit
    end

    it "should be recognized by #for_commit?" do
      @note.should be_for_commit
D
Dmitriy Zaporozhets 已提交
101 102 103
    end
  end

104 105
  describe "Pre-line commit notes" do
    before do
106 107 108 109
      @note = create(:note,
                     noteable_id: commit.id,
                     noteable_type: "Commit",
                     line_code: "0_16_1")
D
Dmitriy Zaporozhets 已提交
110 111 112 113
    end

    it "should save a valid note" do
      @note.noteable_id.should == commit.id
R
Riyad Preukschas 已提交
114 115 116 117 118
      @note.noteable.id.should == commit.id
    end

    it "should be recognized by #for_diff_line?" do
      @note.should be_for_diff_line
D
Dmitriy Zaporozhets 已提交
119 120 121
    end
  end

122
  describe '#create_status_change_note' do
123 124 125
    let(:project)  { create(:project) }
    let(:thing)    { create(:issue, project: project) }
    let(:author)   { create(:user) }
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    let(:status)   { 'new_status' }

    subject { Note.create_status_change_note(thing, author, status) }

    it 'creates and saves a Note' do
      should be_a Note
      subject.id.should_not be_nil
    end

    its(:noteable) { should == thing }
    its(:project)  { should == thing.project }
    its(:author)   { should == author }
    its(:note)     { should =~ /Status changed to #{status}/ }
  end

N
Nihad Abbasov 已提交
141 142
  describe :authorization do
    before do
143
      @p1 = create(:project)
144 145 146 147
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
G
gitlabhq 已提交
148 149 150 151
      @abilities = Six.new
      @abilities << Ability
    end

N
Nihad Abbasov 已提交
152 153
    describe :read do
      before do
154 155
        @p1.users_projects.create(user: @u2, project_access: UsersProject::GUEST)
        @p2.users_projects.create(user: @u3, project_access: UsersProject::GUEST)
G
gitlabhq 已提交
156 157 158 159 160 161 162
      end

      it { @abilities.allowed?(@u1, :read_note, @p1).should be_false }
      it { @abilities.allowed?(@u2, :read_note, @p1).should be_true }
      it { @abilities.allowed?(@u3, :read_note, @p1).should be_false }
    end

N
Nihad Abbasov 已提交
163 164
    describe :write do
      before do
165 166
        @p1.users_projects.create(user: @u2, project_access: UsersProject::DEVELOPER)
        @p2.users_projects.create(user: @u3, project_access: UsersProject::DEVELOPER)
G
gitlabhq 已提交
167 168 169 170 171 172 173
      end

      it { @abilities.allowed?(@u1, :write_note, @p1).should be_false }
      it { @abilities.allowed?(@u2, :write_note, @p1).should be_true }
      it { @abilities.allowed?(@u3, :write_note, @p1).should be_false }
    end

N
Nihad Abbasov 已提交
174 175
    describe :admin do
      before do
176 177 178
        @p1.users_projects.create(user: @u1, project_access: UsersProject::REPORTER)
        @p1.users_projects.create(user: @u2, project_access: UsersProject::MASTER)
        @p2.users_projects.create(user: @u3, project_access: UsersProject::MASTER)
G
gitlabhq 已提交
179 180 181 182 183 184 185 186
      end

      it { @abilities.allowed?(@u1, :admin_note, @p1).should be_false }
      it { @abilities.allowed?(@u2, :admin_note, @p1).should be_true }
      it { @abilities.allowed?(@u3, :admin_note, @p1).should be_false }
    end
  end
end