summaryrefslogtreecommitdiff
path: root/spec/serializers/commit_entity_spec.rb
blob: 35215e06f5f944a91d790577130ac0f38b215b39 (plain)
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
require 'spec_helper'

describe CommitEntity do
  let(:entity) do
    described_class.new(commit, request: request)
  end

  let(:request) { double('request') }
  let(:project) { create(:project, :repository) }
  let(:commit) { project.commit }

  subject { entity.as_json }

  before do
    allow(request).to receive(:project).and_return(project)
  end

  context 'when commit author is a user' do
    before do
      create(:user, email: commit.author_email)
    end

    it 'contains information about user' do
      expect(subject.fetch(:author)).not_to be_nil
    end
  end

  context 'when commit author is not a user' do
    it 'does not contain author details' do
      expect(subject.fetch(:author)).to be_nil
    end
  end

  it 'contains path to commit' do
    expect(subject).to include(:commit_path)
    expect(subject[:commit_path]).to include "commit/#{commit.id}"
  end

  it 'contains URL to commit' do
    expect(subject).to include(:commit_url)
    expect(subject[:commit_path]).to include "commit/#{commit.id}"
  end

  it 'needs to receive project in the request' do
    expect(request).to receive(:project)
      .and_return(project)

    subject
  end

  it 'exposes gravatar url that belongs to author' do
    expect(subject.fetch(:author_gravatar_url)).to match /gravatar/
  end

  context 'when type is not set' do
    it 'does not expose extra properties' do
      expect(subject).not_to include(:description_html)
      expect(subject).not_to include(:title_html)
    end
  end

  context 'when type is "full"' do
    let(:entity) do
      described_class.new(commit, request: request, type: :full)
    end

    it 'exposes extra properties' do
      expect(subject).to include(:description_html)
      expect(subject).to include(:title_html)
      expect(subject.fetch(:description_html)).not_to be_nil
      expect(subject.fetch(:title_html)).not_to be_nil
    end
  end

  context 'when commit_url_params is set' do
    let(:entity) do
      params = { merge_request_iid: 3 }

      described_class.new(commit, request: request, commit_url_params: params)
    end

    it 'adds commit_url_params to url and path' do
      expect(subject[:commit_path]).to include "?merge_request_iid=3"
      expect(subject[:commit_url]).to include "?merge_request_iid=3"
    end
  end
end