Skip to content

Commit

Permalink
Fix public analytics API endpoint after Rails 4 upgrade.
Browse files Browse the repository at this point in the history
The mongoid caching gem we were using wasn't actually compatible with
Rails 4. This should fix it, and add test coverage for this private/beta
API endpoint.
  • Loading branch information
GUI committed Jun 15, 2017
1 parent 749dfe8 commit 3187452
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/api-umbrella/web-app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ gem "mongoid_rails_migrations", "~> 1.1.0"
# Rails cache store using mongo.
#
# Use master from git for Rails 4 compatibility.
gem "mongoid-store", :git => "https://github.com/ahoward/mongoid-store.git"
gem "mongoid_store", :git => "https://github.com/lucianoshl/mongoid_store.git"

# Elasticsearch
gem "elasticsearch", "~> 2.0.2"
Expand Down
18 changes: 9 additions & 9 deletions src/api-umbrella/web-app/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ GIT
addressable (~> 2.3)
omniauth (~> 1.2)

GIT
remote: https://github.com/ahoward/mongoid-store.git
revision: 7b7e34e263a1e25fc0149861e1560f7f483c16ac
specs:
mongoid-store (0.5.0)
activesupport (>= 3)
mongoid (>= 3)

GIT
remote: https://github.com/heroku/rails_stdout_logging.git
revision: 92fb79ed040a88808aaf5bcb5e88ae8d85176a85
specs:
rails_stdout_logging (0.0.5)

GIT
remote: https://github.com/lucianoshl/mongoid_store.git
revision: 17053a38482c6ab41f4457a96a488041cd6c8c3c
specs:
mongoid_store (0.1.2)
activesupport (> 3.0)
mongoid (> 3.0)

GIT
remote: https://github.com/zerowidth/lucene_query_parser.git
revision: ef723c40103203a55f85b3ec02743b17d4d5e773
Expand Down Expand Up @@ -315,9 +315,9 @@ DEPENDENCIES
mongoid (~> 5.2.0)
mongoid-embedded-errors (~> 2.1.1)
mongoid-paranoia (~> 2.0.0)
mongoid-store!
mongoid_delorean (~> 1.3.0)
mongoid_rails_migrations (~> 1.1.0)
mongoid_store!
multi_json (~> 1.12.1)
nokogiri (~> 1.8.0)
oj (~> 3.1.3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ def summary

# Try to fetch the summary data out of the cache.
summary = Rails.cache.read("analytics_summary")
if(summary)
headers["X-Cache"] = "HIT"
end

# If it's not cached, generate it now.
if(!summary || !summary[:cached_at])
headers["X-Cache"] = "MISS"
summary = generate_summary
Rails.cache.write("analytics_summary", summary, :expires_in => 2.days)

Expand Down
95 changes: 95 additions & 0 deletions test/apis/v0/test_analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require_relative "../../test_helper"

class Test::Apis::V0::TestAnalytics < Minitest::Test
include ApiUmbrellaTestHelpers::Setup

def setup
super
setup_server
ApiUser.where(:registration_source.ne => "seed").delete_all
ElasticsearchHelper.clean_es_indices(["2013-06", "2013-07"])

@db = Mongoid.client(:default)
@db[:rails_cache].delete_many
end

def test_forbids_api_key_without_role
user = FactoryGirl.create(:api_user, {
:roles => ["api-umbrella-public-metricsx"],
})

response = make_request(user)
assert_response_code(401, response)
end

def test_allows_api_key_with_role
response = make_request
assert_response_code(200, response)
end

def test_expected_response
FactoryGirl.create_list(:api_user, 3, :created_at => Time.parse("2013-07-15T00:00:00Z").utc)
FactoryGirl.create_list(:log_item, 2, :request_at => Time.parse("2013-07-15T00:00:00Z").utc)
LogItem.gateway.refresh_index!

response = make_request
assert_response_code(200, response)
assert_equal("MISS", response.headers["X-Cache"])

data = MultiJson.load(response.body)
assert_operator(data["total_hits"], :>, 0)
assert_operator(data["total_users"], :>, 0)
assert_kind_of(Array, data["hits_by_month"])
assert_kind_of(Array, data["users_by_month"])

assert_equal({
"year" => 2013,
"month" => 6,
"count" => 0,
}, data["hits_by_month"][0])
assert_equal({
"year" => 2013,
"month" => 7,
"count" => 2,
}, data["hits_by_month"][1])

assert_equal({
"year" => 2013,
"month" => 6,
"count" => 0,
}, data["users_by_month"][0])
assert_equal({
"year" => 2013,
"month" => 7,
"count" => 3,
}, data["users_by_month"][1])
end

def test_caches_results
assert_equal(0, @db[:rails_cache].count)

response = make_request
assert_equal("MISS", response.headers["X-Cache"])
assert_equal(1, @db[:rails_cache].count)

response = make_request
assert_equal("HIT", response.headers["X-Cache"])
assert_equal(1, @db[:rails_cache].count)

cache = @db[:rails_cache].find.first
assert_equal("analytics_summary", cache["_id"])
assert_in_delta(Time.now.to_i, cache["created_at"], 10)
assert_in_delta(Time.now.to_i + 60 * 60 * 24 * 2, cache["expires_at"], 10)
assert(cache["data"])
end

private

def make_request(user = nil)
user ||= FactoryGirl.create(:api_user, :roles => ["api-umbrella-public-metrics"])

Typhoeus.get("https://127.0.0.1:9081/api-umbrella/v0/analytics/summary.json", http_options.deep_merge({
:headers => { "X-Api-Key" => user.api_key },
}))
end
end
2 changes: 1 addition & 1 deletion test/support/api_umbrella_test_helpers/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def setup_server

# For simplicity sake, we're assuming our tests only deal with a few explicit
# indexes currently.
["2014-11", "2015-01", "2015-03"].each do |month|
["2013-06", "2013-07", "2014-11", "2015-01", "2015-03"].each do |month|
# First delete any existing indexes.
["api-umbrella-logs-v1-#{month}", "api-umbrella-logs-#{month}", "api-umbrella-logs-write-#{month}"].each do |index_name|
begin
Expand Down
2 changes: 2 additions & 0 deletions test/support/test_namespaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Admin
module Stats; end
end

module V0; end

module V1
module AdminGroups; end
module AdminPermissions; end
Expand Down

0 comments on commit 3187452

Please sign in to comment.