• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NYULibraries / salon / #219

01 Nov 2024 05:48PM UTC coverage: 67.887% (-29.6%) from 97.504%
#219

push

408 of 601 relevant lines covered (67.89%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.41
/spec/controllers/resource_controller_spec.rb
1
require 'spec_helper'
1✔
2

3
describe 'ResourceController' do
1✔
4
  def app() ResourceController end
1✔
5

6
  subject { last_response }
1✔
7

8
  let(:redis){ spy('redis', get: nil, set: nil, keys: []) }
1✔
9
  before { allow(RedisObject).to receive(:redis).and_return redis }
1✔
10

11
  describe "GET /" do
1✔
12
    before { get "/" }
1✔
13
    it { is_expected.to be_not_found }
1✔
14
  end
15

16
  describe "GET /:identifier" do
1✔
17
    let(:example_site) { 'https://www.example.com/' }
1✔
18
    before do
1✔
19
      allow(redis).to receive(:get).with('good_identifier').and_return example_site
×
20
      get "/#{identifier}"
×
21
    end
22
    context 'if an identifier is passed in' do
1✔
23
      context 'and the identifier is a key in the cache' do
1✔
24
        let(:identifier) {'good_identifier'}
1✔
25
        context 'and the identifier has a non-blank url value' do
1✔
26
          it 'should be a redirect to the cache value' do
1✔
27
            expect(last_response).to be_redirect
×
28
            follow_redirect!
×
29
            expect(last_request.url).to eql example_site
×
30
          end
31
        end
32
        context 'but the identifier has a blank url value' do
1✔
33
          let(:example_site) { '' }
1✔
34
          it { is_expected.to be_bad_request }
1✔
35
        end
36
      end
37
      context 'and the identifier is not a key in the cache' do
1✔
38
        let(:identifier) {'bad_identifier'}
1✔
39
        it { is_expected.to be_bad_request }
1✔
40
      end
41
    end
42
  end
43

44
  describe "GET /healthcheck" do
1✔
45
    before { get "/healthcheck" }
1✔
46
    its(:status) { is_expected.to eq 200 }
1✔
47
    its(:body) { is_expected.to eq "{\"success\":true}" }
1✔
48
  end
49

50
  describe "POST /" do
1✔
51
    let(:basic_token) { ENV['SALON_BASIC_AUTH_TOKEN'] || 'access_token' }
1✔
52
    let(:data) { nil }
1✔
53
    context 'when bearer token is sent in authorization header' do
1✔
54
      before { allow(redis).to receive(:set).and_return true }
1✔
55
      before { post "/", data, {'HTTP_AUTHORIZATION' => "Basic #{basic_token}"} }
1✔
56
      subject { last_response }
1✔
57

58
      context 'and access token is valid for admin' do
1✔
59
        let(:basic_token) { ENV['SALON_ADMIN_BASIC_AUTH_TOKEN'] || 'admin_access_token' }
1✔
60
        let(:data) { '{"id":"key","url":"http://value.com"}' }
1✔
61
        its(:status) { is_expected.to eql 201 }
1✔
62
        its(:body) { is_expected.to eql data }
1✔
63
      end
64

65
      context 'and access token is valid for non-admin' do
1✔
66
        context 'but data is invalid' do
1✔
67
          its(:status) { is_expected.to eql 400 }
1✔
68
          its(:body) { is_expected.to include "Invalid JSON" }
1✔
69
        end
70
        context 'and data is valid' do
1✔
71
          let(:data) { '{"id":"key","url":"http://value.com"}' }
1✔
72
          its(:status) { is_expected.to eql 201 }
1✔
73
          its(:body) { is_expected.to eql data }
1✔
74
          it "should save data" do
1✔
75
            expect(redis).to have_received(:set).with("key", "http://value.com")
×
76
          end
77
        end
78
      end
79
    end
80
    context 'when bearer token is NOT sent' do
1✔
81
      before { post "/" }
1✔
82
      its(:status) { is_expected.to eql 401 }
1✔
83
      its(:body) { is_expected.to eql '{"error":"Unauthorized: The user does not have sufficient privileges to perform this action."}' }
1✔
84
    end
85
  end
86

87
  describe "POST /create_with_array" do
1✔
88
    # tested in dredd
89
  end
90

91
  describe "POST /create_empty_resource" do
1✔
92
    # tested in dredd
93
  end
94

95
  describe "POST /reset_with_array" do
1✔
96
    let(:data) { nil }
1✔
97
    let(:basic_token) { ENV['SALON_BASIC_AUTH_TOKEN'] || 'access_token' }
1✔
98
    context 'when bearer token is sent in authorization header' do
1✔
99
      before { post "/reset_with_array", data, {'HTTP_AUTHORIZATION' => "Basic #{basic_token}"} }
1✔
100
      subject { last_response }
1✔
101

102
      context 'and access token is valid for non-admin' do
1✔
103
        its(:status) { is_expected.to eql 401 }
1✔
104
      end
105
      context 'and access token is valid for admin' do
1✔
106
        let(:basic_token) { ENV['SALON_ADMIN_BASIC_AUTH_TOKEN'] || 'admin_access_token' }
1✔
107
        context 'but data is invalid' do
1✔
108
          its(:status) { is_expected.to eql 400 }
1✔
109
          its(:body) { is_expected.to include "Invalid JSON"}
1✔
110
        end
111
        context 'and data is valid' do
1✔
112
          let(:data) { '[{"id":"key","url":"http://value.com"},{"id":"key2","url":"http://value2.org"}]' }
1✔
113
          its(:status) { is_expected.to eql 201 }
1✔
114
          its(:body) { is_expected.to eql data }
1✔
115
          it "should save data" do
1✔
116
            expect(redis).to have_received(:set).with("key", "http://value.com").ordered
×
117
            expect(redis).to have_received(:set).with("key2", "http://value2.org").ordered
×
118
            expect(redis).to_not have_received(:del)
×
119
          end
120
        end
121
      end
122
    end
123
    context 'when bearer token is NOT sent' do
1✔
124
      before { post "/" }
1✔
125
      its(:status) { is_expected.to eql 401 }
1✔
126
      its(:body) { is_expected.to eql '{"error":"Unauthorized: The user does not have sufficient privileges to perform this action."}' }
1✔
127
    end
128
  end
129
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc