feat: Add upload under account scope (#7914)
This commit is contained in:
parent
2429daa45c
commit
53d530b815
@ -1,4 +1,4 @@
|
|||||||
class Api::V1::UploadController < Api::BaseController
|
class Api::V1::Accounts::UploadController < Api::V1::Accounts::BaseController
|
||||||
def create
|
def create
|
||||||
file_blob = ActiveStorage::Blob.create_and_upload!(
|
file_blob = ActiveStorage::Blob.create_and_upload!(
|
||||||
key: nil,
|
key: nil,
|
||||||
@ -25,10 +25,10 @@ describe('#Upload Helpers', () => {
|
|||||||
|
|
||||||
axios.post.mockResolvedValueOnce(mockResponse);
|
axios.post.mockResolvedValueOnce(mockResponse);
|
||||||
|
|
||||||
const result = await uploadFile(mockFile);
|
const result = await uploadFile(mockFile, '1602');
|
||||||
|
|
||||||
expect(axios.post).toHaveBeenCalledWith(
|
expect(axios.post).toHaveBeenCalledWith(
|
||||||
'/api/v1/upload',
|
'/api/v1/accounts/1602/upload',
|
||||||
expect.any(FormData),
|
expect.any(FormData),
|
||||||
{ headers: { 'Content-Type': 'multipart/form-data' } }
|
{ headers: { 'Content-Type': 'multipart/form-data' } }
|
||||||
);
|
);
|
||||||
|
|||||||
@ -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).
|
* @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.
|
* @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.
|
// Create a new FormData instance.
|
||||||
let formData = new FormData();
|
let formData = new FormData();
|
||||||
|
|
||||||
|
if (!accountId) {
|
||||||
|
accountId = window.location.pathname.split('/')[3];
|
||||||
|
}
|
||||||
|
|
||||||
// Append the file to the FormData instance under the key 'attachment'.
|
// Append the file to the FormData instance under the key 'attachment'.
|
||||||
formData.append('attachment', file);
|
formData.append('attachment', file);
|
||||||
|
|
||||||
// Use axios to send a POST request to the upload endpoint.
|
// Use axios to send a POST request to the upload endpoint.
|
||||||
const { data } = await axios.post(`/api/${API_VERSION}/upload`, formData, {
|
const { data } = await axios.post(
|
||||||
headers: HEADERS,
|
`/api/${API_VERSION}/accounts/${accountId}/upload`,
|
||||||
});
|
formData,
|
||||||
|
{
|
||||||
|
headers: HEADERS,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fileUrl: data.file_url,
|
fileUrl: data.file_url,
|
||||||
|
|||||||
@ -138,8 +138,9 @@ class Rack::Attack
|
|||||||
end
|
end
|
||||||
|
|
||||||
## Prevent Abuse of attachment upload APIs ##
|
## Prevent Abuse of attachment upload APIs ##
|
||||||
throttle('/api/v1/upload', limit: 60, period: 1.hour) do |req|
|
throttle('/api/v1/accounts/:account_id/upload', limit: 60, period: 1.hour) do |req|
|
||||||
req.ip if req.path_without_extentions == '/api/v1/upload' && req.post?
|
match_data = %r{/api/v1/accounts/(?<account_id>\d+)/upload}.match(req.path)
|
||||||
|
match_data[:account_id] if match_data.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
## ----------------------------------------------- ##
|
## ----------------------------------------------- ##
|
||||||
|
|||||||
@ -217,13 +217,13 @@ Rails.application.routes.draw do
|
|||||||
post :reorder, on: :collection
|
post :reorder, on: :collection
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :upload, only: [:create]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# end of account scoped api routes
|
# end of account scoped api routes
|
||||||
# ----------------------------------
|
# ----------------------------------
|
||||||
|
|
||||||
resources :upload, only: [:create]
|
|
||||||
|
|
||||||
namespace :integrations do
|
namespace :integrations do
|
||||||
resources :webhooks, only: [:create]
|
resources :webhooks, only: [:create]
|
||||||
end
|
end
|
||||||
|
|||||||
@ -128,7 +128,7 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
|
|||||||
|
|
||||||
expect(account.automation_rules.count).to eq(0)
|
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,
|
headers: administrator.create_new_auth_token,
|
||||||
params: { attachment: file }
|
params: { attachment: file }
|
||||||
|
|
||||||
@ -163,13 +163,13 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
|
|||||||
file_1 = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
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')
|
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,
|
headers: administrator.create_new_auth_token,
|
||||||
params: { attachment: file_1 }
|
params: { attachment: file_1 }
|
||||||
|
|
||||||
blob_1 = response.parsed_body
|
blob_1 = response.parsed_body
|
||||||
|
|
||||||
post '/api/v1/upload',
|
post "/api/v1/accounts/#{account.id}/upload/",
|
||||||
headers: administrator.create_new_auth_token,
|
headers: administrator.create_new_auth_token,
|
||||||
params: { attachment: file_2 }
|
params: { attachment: file_2 }
|
||||||
|
|
||||||
|
|||||||
@ -129,7 +129,7 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
|
|||||||
it 'Saves file in the macros actions to send an attachments' do
|
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')
|
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,
|
headers: administrator.create_new_auth_token,
|
||||||
params: { attachment: file }
|
params: { attachment: file }
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe 'Api::V1::UploadController', type: :request do
|
RSpec.describe 'Api::V1::Accounts::UploadController', type: :request do
|
||||||
describe 'POST /api/v1/upload/' do
|
describe 'POST /api/v1/account/1/upload/' do
|
||||||
let(:account) { create(:account) }
|
let(:account) { create(:account) }
|
||||||
let(:user) { create(:user, account: account) }
|
let(:user) { create(:user, account: account) }
|
||||||
|
|
||||||
it 'uploads the image when authorized' do
|
it 'uploads the image when authorized' do
|
||||||
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
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,
|
headers: user.create_new_auth_token,
|
||||||
params: { attachment: file }
|
params: { attachment: file }
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ RSpec.describe 'Api::V1::UploadController', type: :request do
|
|||||||
it 'does not upload when un-authorized' do
|
it 'does not upload when un-authorized' do
|
||||||
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
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: {},
|
headers: {},
|
||||||
params: { attachment: file }
|
params: { attachment: file }
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user