-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from turingschool/feature/private-links
Feature/private links
- Loading branch information
Showing
10 changed files
with
163 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class LinkSerializer | ||
include JSONAPI::Serializer | ||
attributes :original, :short, :user_id, :tags, :click_count, :last_click | ||
attributes :original, :short, :user_id, :tags, :click_count, :last_click, :private | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddPrivateToLinks < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :links, :private, :boolean, default: false | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,43 @@ | |
it { should have_many(:tags).through(:link_tags) } | ||
end | ||
|
||
describe 'class methods' do | ||
describe 'methods' do | ||
describe 'create_new' do | ||
it 'can create a new link' do | ||
user1 = User.create(email: "[email protected]", password: "user123") | ||
link = Link.create_new(user1.id, "long-link-example.com") | ||
user1 = User.create(email: '[email protected]', password: 'user123') | ||
link = Link.create_new(user1.id, 'long-link-example.com') | ||
|
||
expect(link).to be_a Link | ||
expect(link.user_id).to eq(user1.id) | ||
expect(link.original).to eq("long-link-example.com") | ||
expect(link.original).to eq('long-link-example.com') | ||
expect(link.short).to be_a String | ||
end | ||
end | ||
|
||
describe '.create_short_link' do | ||
it 'creates a unique short link' do | ||
short1 = Link.create_short_link | ||
short2 = Link.create_short_link | ||
expect(short1).not_to eq(short2) | ||
end | ||
end | ||
|
||
describe '#update_privacy' do | ||
it 'updates the privacy setting' do | ||
user = User.create!(email: '[email protected]', password: 'password') | ||
link = Link.create!(user:, original: 'https://example.com', short: 'tur.link/abc123', private: false) | ||
link.update_privacy(true) | ||
expect(link.reload.private).to be true | ||
end | ||
end | ||
|
||
describe 'default scope' do | ||
it 'excludes disabled links' do | ||
user = User.create!(email: '[email protected]', password: 'password') | ||
Link.create!(user:, original: 'https://example1.com', short: 'tur.link/abc123', disabled: false) | ||
Link.create!(user:, original: 'https://example2.com', short: 'tur.link/def456', disabled: true) | ||
expect(Link.count).to eq(1) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,4 +230,52 @@ | |
expect(links.last[:attributes][:click_count]).to eq(50) | ||
end | ||
end | ||
|
||
describe 'PATCH /users/:user_id/links/:id/update_privacy' do | ||
let(:user) { User.create(email: '[email protected]', password: 'password') } | ||
let(:link) { Link.create(original: 'https://example.com', short: 'tur.link/abc123', user:) } | ||
|
||
it 'updates the privacy setting of a link' do | ||
patch "/api/v1/users/#{user.id}/links/#{link.id}/update_privacy", params: { private: 'true' } | ||
|
||
expect(response).to be_successful | ||
expect(response.status).to eq(200) | ||
|
||
json_response = JSON.parse(response.body, symbolize_names: true) | ||
expect(json_response[:message]).to eq('Privacy setting updated successfully') | ||
|
||
link.reload | ||
expect(link.private).to be true | ||
end | ||
|
||
it 'returns an error if the user does not own the link' do | ||
other_user = User.create(email: '[email protected]', password: 'password') | ||
patch "/api/v1/users/#{other_user.id}/links/#{link.id}/update_privacy", params: { private: 'true' } | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
|
||
json_response = JSON.parse(response.body, symbolize_names: true) | ||
expect(json_response[:error]).to eq('Unauthorized to update this link') | ||
end | ||
|
||
it 'returns an error if the link does not exist' do | ||
patch "/api/v1/users/#{user.id}/links/9999/update_privacy", params: { private: 'true' } | ||
|
||
expect(response).to have_http_status(:not_found) | ||
|
||
json_response = JSON.parse(response.body, symbolize_names: true) | ||
expect(json_response[:error]).to eq('User or Link not found') | ||
end | ||
|
||
it 'returns an error if the update fails' do | ||
allow_any_instance_of(Link).to receive(:update).and_return(false) | ||
|
||
patch "/api/v1/users/#{user.id}/links/#{link.id}/update_privacy", params: { private: 'true' } | ||
|
||
expect(response).to have_http_status(:unprocessable_entity) | ||
|
||
json_response = JSON.parse(response.body, symbolize_names: true) | ||
expect(json_response[:error]).to eq('Failed to update privacy setting') | ||
end | ||
end | ||
end |