Skip to content

Commit

Permalink
feat: Add upload under account scope (#7914)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmmishra committed Sep 19, 2023
1 parent 2429daa commit 53d530b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
@@ -1,4 +1,4 @@
class Api::V1::UploadController < Api::BaseController
class Api::V1::Accounts::UploadController < Api::V1::Accounts::BaseController
def create
file_blob = ActiveStorage::Blob.create_and_upload!(
key: nil,
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/dashboard/helper/specs/uploadHelper.spec.js
Expand Up @@ -25,10 +25,10 @@ describe('#Upload Helpers', () => {

axios.post.mockResolvedValueOnce(mockResponse);

const result = await uploadFile(mockFile);
const result = await uploadFile(mockFile, '1602');

expect(axios.post).toHaveBeenCalledWith(
'/api/v1/upload',
'/api/v1/accounts/1602/upload',
expect.any(FormData),
{ headers: { 'Content-Type': 'multipart/form-data' } }
);
Expand Down
16 changes: 12 additions & 4 deletions app/javascript/dashboard/helper/uploadHelper.js
Expand Up @@ -21,17 +21,25 @@ const HEADERS = {
* @param {File} file - The file to be uploaded. It should be a File object (typically coming from a file input element).
* @returns {Promise} A promise that resolves with the server's response when the upload is successful, or rejects if there's an error.
*/
export async function uploadFile(file) {
export async function uploadFile(file, accountId) {
// Create a new FormData instance.
let formData = new FormData();

if (!accountId) {
accountId = window.location.pathname.split('/')[3];
}

// Append the file to the FormData instance under the key 'attachment'.
formData.append('attachment', file);

// Use axios to send a POST request to the upload endpoint.
const { data } = await axios.post(`/api/${API_VERSION}/upload`, formData, {
headers: HEADERS,
});
const { data } = await axios.post(
`/api/${API_VERSION}/accounts/${accountId}/upload`,
formData,
{
headers: HEADERS,
}
);

return {
fileUrl: data.file_url,
Expand Down
5 changes: 3 additions & 2 deletions config/initializers/rack_attack.rb
Expand Up @@ -138,8 +138,9 @@ def path_without_extentions
end

## Prevent Abuse of attachment upload APIs ##
throttle('/api/v1/upload', limit: 60, period: 1.hour) do |req|
req.ip if req.path_without_extentions == '/api/v1/upload' && req.post?
throttle('/api/v1/accounts/:account_id/upload', limit: 60, period: 1.hour) do |req|
match_data = %r{/api/v1/accounts/(?<account_id>\d+)/upload}.match(req.path)
match_data[:account_id] if match_data.present?
end

## ----------------------------------------------- ##
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Expand Up @@ -217,13 +217,13 @@
post :reorder, on: :collection
end
end

resources :upload, only: [:create]
end
end
# end of account scoped api routes
# ----------------------------------

resources :upload, only: [:create]

namespace :integrations do
resources :webhooks, only: [:create]
end
Expand Down
Expand Up @@ -128,7 +128,7 @@

expect(account.automation_rules.count).to eq(0)

post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file }

Expand Down Expand Up @@ -163,13 +163,13 @@
file_1 = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
file_2 = fixture_file_upload(Rails.root.join('spec/assets/sample.png'), 'image/png')

post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file_1 }

blob_1 = response.parsed_body

post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file_2 }

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/v1/accounts/macros_controller_spec.rb
Expand Up @@ -129,7 +129,7 @@
it 'Saves file in the macros actions to send an attachments' do
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')

post '/api/v1/upload',
post "/api/v1/accounts/#{account.id}/upload/",
headers: administrator.create_new_auth_token,
params: { attachment: file }

Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/api/v1/upload_controller_spec.rb
@@ -1,14 +1,14 @@
require 'rails_helper'

RSpec.describe 'Api::V1::UploadController', type: :request do
describe 'POST /api/v1/upload/' do
RSpec.describe 'Api::V1::Accounts::UploadController', type: :request do
describe 'POST /api/v1/account/1/upload/' do
let(:account) { create(:account) }
let(:user) { create(:user, account: account) }

it 'uploads the image when authorized' do
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')

post '/api/v1/upload/',
post "/api/v1/accounts/#{account.id}/upload/",
headers: user.create_new_auth_token,
params: { attachment: file }

Expand All @@ -25,7 +25,7 @@
it 'does not upload when un-authorized' do
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')

post '/api/v1/upload/',
post "/api/v1/accounts/#{account.id}/upload/",
headers: {},
params: { attachment: file }

Expand Down

0 comments on commit 53d530b

Please sign in to comment.