Skip to content

Commit

Permalink
Rework API key into token per query
Browse files Browse the repository at this point in the history
  • Loading branch information
abuisman committed Sep 11, 2023
1 parent fd9f4d6 commit 4b29bcb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/blazer/sharing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def enabled?
enabled
end

def share_path(query_id, format: nil)
def share_path(query_id, format: nil, token: nil)
query = Query.find(query_id)
"#{path}/#{query.secret_token}/#{query_id}#{".#{format}" if format}"
"#{path}/#{token}/#{query_id}#{".#{format}" if format}"
end

def url_for(query_id, current_url, format: 'csv')
Expand Down
4 changes: 4 additions & 0 deletions test/internal/config/blazer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,7 @@ uploads:
url: postgres://localhost/blazer_test
schema: uploads
data_source: main

sharing:
path: /blazer_share
enabled: true
2 changes: 2 additions & 0 deletions test/internal/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
mount Blazer::Engine, at: "/"

get Blazer.sharing.route_path, to: Blazer.sharing.to_controller, as: :share_query if Blazer.sharing.enabled?
end
14 changes: 10 additions & 4 deletions test/queries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,18 @@ def test_variables_time_range

def test_correct_token
query = create_query(statement: "SELECT 1")
get blazer.query_path(query, token: query.secret_token)
get share_query_path(query.id, token: query.secret_token, format: 'csv')

assert_response :success
assert_equal "text/csv", response.content_type
end

get blazer.query_path(query, token: "x")
assert_response :redirect
def test_incorrect_token
query = create_query(statement: "SELECT 1")
get share_query_path(query.id, token: "x")

assert_response :forbidden
assert_match "Access denied", response.body
end

def test_variable_defaults
Expand Down Expand Up @@ -123,7 +130,6 @@ def test_share
get blazer.query_share_path(query_id: query.id, token: query.secret_token, format: 'csv')

assert_response :success
assert_match query.name, response.body
end

def test_url
Expand Down

0 comments on commit 4b29bcb

Please sign in to comment.