about summary refs log tree commit diff
path: root/spec/controllers/api/v1/tags_controller_spec.rb
blob: 216faad8724a43904b3b17a83762e048a58c68f1 (plain) (blame)
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
require 'rails_helper'

RSpec.describe Api::V1::TagsController, type: :controller do
  render_views

  let(:user)   { Fabricate(:user) }
  let(:scopes) { 'write:follows' }
  let(:token)  { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }

  before { allow(controller).to receive(:doorkeeper_token) { token } }

  describe 'GET #show' do
    before do
      get :show, params: { id: name }
    end

    context 'with existing tag' do
      let!(:tag) { Fabricate(:tag) }
      let(:name) { tag.name }

      it 'returns http success' do
        expect(response).to have_http_status(:success)
      end
    end

    context 'with non-existing tag' do
      let(:name) { 'hoge' }

      it 'returns http success' do
        expect(response).to have_http_status(:success)
      end
    end
  end

  describe 'POST #follow' do
    let!(:unrelated_tag) { Fabricate(:tag) }

    before do
      TagFollow.create!(account: user.account, tag: unrelated_tag)

      post :follow, params: { id: name }
    end

    context 'with existing tag' do
      let!(:tag) { Fabricate(:tag) }
      let(:name) { tag.name }

      it 'returns http success' do
        expect(response).to have_http_status(:success)
      end

      it 'creates follow' do
        expect(TagFollow.where(tag: tag, account: user.account).exists?).to be true
      end
    end

    context 'with non-existing tag' do
      let(:name) { 'hoge' }

      it 'returns http success' do
        expect(response).to have_http_status(:success)
      end

      it 'creates follow' do
        expect(TagFollow.where(tag: Tag.find_by!(name: name), account: user.account).exists?).to be true
      end
    end
  end

  describe 'POST #unfollow' do
    let!(:tag) { Fabricate(:tag, name: 'foo') }
    let!(:tag_follow) { Fabricate(:tag_follow, account: user.account, tag: tag) }

    before do
      post :unfollow, params: { id: tag.name }
    end

    it 'returns http success' do
      expect(response).to have_http_status(:success)
    end

    it 'removes the follow' do
      expect(TagFollow.where(tag: tag, account: user.account).exists?).to be false
    end
  end
end