-
Notifications
You must be signed in to change notification settings - Fork 2
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 #157 from Ches-ctrl/2024-05-07-add-post-api
2024 05 07 add post api
- Loading branch information
Showing
8 changed files
with
149 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Api | ||
module V0 | ||
class JobsController < ApplicationController | ||
skip_before_action :verify_authenticity_token, only: :add_job | ||
before_action :authenticate_user! | ||
before_action :authenticate_with_api_key | ||
before_action :verify_request_origin | ||
|
||
def add_job | ||
posting_url = params[:posting_url] | ||
|
||
if posting_url.present? | ||
process_job_posting(posting_url) | ||
else | ||
render json: { message: 'Post API connected but no posting_url' }, status: :bad_request | ||
end | ||
end | ||
|
||
private | ||
|
||
def authenticate_with_api_key | ||
api_key = request.headers['X-Api-Key'] | ||
render json: { error: 'Unauthorized API key' }, status: :unauthorized unless valid_api_key?(api_key) | ||
end | ||
|
||
def valid_api_key?(api_key) | ||
api_key == ENV.fetch('CHROME_EXTENSION_API_KEY') | ||
end | ||
|
||
def verify_request_origin | ||
origin = request.headers['Origin'] | ||
render json: { error: 'Unauthorized origin' }, status: :unauthorized unless valid_origin?(origin) | ||
end | ||
|
||
def valid_origin?(origin) | ||
origin == ENV.fetch('CHROME_EXTENSION_ORIGIN') | ||
end | ||
|
||
def process_job_posting(posting_url) | ||
render json: { message: 'Received but not a GH job' }, status: :ok and return unless posting_url.include?('greenhouse') | ||
|
||
Rails.logger.info("Processing job posting: #{posting_url}") | ||
|
||
if CreateJobFromUrl.perform_later(posting_url) | ||
render json: { message: 'Job creation queued successfully' }, status: :ok | ||
else | ||
render json: { error: 'Failed to queue job creation' }, status: :unprocessable_entity | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Rails.application.config.middleware.insert_before 0, Rack::Cors do | ||
allow do | ||
origins 'chrome-extension://ioffpdelmlddmepimkcadfmmhfhocgkm' | ||
resource '/api/v0/add_job', headers: :any, methods: [:post] | ||
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
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,75 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Api::V0::JobsController, type: :controller, api: true do | ||
describe 'POST #add_job' do | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
context 'with valid API key, origin, and user session' do | ||
before do | ||
sign_in user | ||
request.headers['X-Api-Key'] = ENV.fetch('CHROME_EXTENSION_API_KEY') | ||
request.headers['Origin'] = ENV.fetch('CHROME_EXTENSION_ORIGIN') | ||
post :add_job, params: { posting_url: 'https://example.com/job' } | ||
end | ||
|
||
it 'returns a successful response' do | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it 'returns a JSON response with a success message' do | ||
json_response = JSON.parse(response.body) | ||
expect(json_response['message']).to eq('Received but not a GH job') | ||
end | ||
end | ||
|
||
context 'without user session' do | ||
before do | ||
request.headers['X-Api-Key'] = ENV.fetch('CHROME_EXTENSION_API_KEY') | ||
request.headers['Origin'] = ENV.fetch('CHROME_EXTENSION_ORIGIN') | ||
post :add_job, params: { posting_url: 'https://example.com/job' } | ||
end | ||
|
||
it 'returns found' do | ||
expect(response).to have_http_status(:found) | ||
end | ||
end | ||
|
||
context 'with invalid API key' do | ||
before do | ||
sign_in user | ||
request.headers['X-Api-Key'] = 'invalid_api_key' | ||
request.headers['Origin'] = ENV.fetch('CHROME_EXTENSION_ORIGIN') | ||
end | ||
|
||
it 'returns an unauthorized response' do | ||
post :add_job | ||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
|
||
it 'returns a JSON response with an error message' do | ||
post :add_job | ||
json_response = JSON.parse(response.body) | ||
expect(json_response['error']).to eq('Unauthorized API key') | ||
end | ||
end | ||
|
||
context 'with invalid origin' do | ||
before do | ||
sign_in user | ||
request.headers['X-Api-Key'] = ENV.fetch('CHROME_EXTENSION_API_KEY') | ||
request.headers['Origin'] = 'invalid_origin' | ||
end | ||
|
||
it 'returns an unauthorized response' do | ||
post :add_job | ||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
|
||
it 'returns a JSON response with an error message' do | ||
post :add_job | ||
json_response = JSON.parse(response.body) | ||
expect(json_response['error']).to eq('Unauthorized origin') | ||
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