Skip to content

Commit

Permalink
add tests for activities. #374
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 6, 2020
1 parent 1ca0af5 commit e7cb0c5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def index
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
sort: params[:sort] }.compact.to_query,
}.compact
options[:include] = @include
options[:is_collection] = true
Expand Down Expand Up @@ -103,6 +103,6 @@ def set_include
def set_activity
response = Activity.find_by_id(params[:id])
@activity = response.results.first
fail ActiveRecord::RecordNotFound unless @activity.present?
fail ActiveRecord::RecordNotFound if @activity.blank?
end
end
11 changes: 5 additions & 6 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def self.import_by_ids(options={})
end

def self.import_by_id(options={})
return nil unless options[:id].present?
return nil if options[:id].blank?

id = options[:id].to_i
index = Rails.env.test? ? "activities-test" : self.inactive_index
Expand All @@ -228,7 +228,7 @@ def self.import_by_id(options={})

if errors > 1
Rails.logger.error "[Elasticsearch] #{errors} errors importing #{count} activities with IDs #{id} - #{(id + 499)}."
elsif count > 0
elsif count.positive?
Rails.logger.info "[Elasticsearch] Imported #{count} activities with IDs #{id} - #{(id + 499)}."
end

Expand Down Expand Up @@ -291,19 +291,18 @@ def self.convert_affiliation_by_id(options={})
contributors = Array.wrap(audited_changes["contributors"]).map do |c|
# c is an array if there are changes
return [] if c.blank?

c = c.last if c.is_a?(Array)

if c["affiliation"].nil?
c["affiliation"] = []
should_update = true
elsif c["affiliation"].is_a?(String)
c["affiliation"] = [{ "name" => c["affiliation"] }]
should_update = true
else c["affiliation"].is_a?(Hash)
c["affiliation"] = Array.wrap(c["affiliation"])
should_update = true
end

should_update = true
c
end

Expand All @@ -314,7 +313,7 @@ def self.convert_affiliation_by_id(options={})
count += 1
end
end

Rails.logger.info "[Elasticsearch] Converted affiliations for #{count} activities with IDs #{id} - #{(id + 499)}." if count > 0

count
Expand Down
8 changes: 4 additions & 4 deletions app/serializers/activity_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class ActivitySerializer
end

attribute "prov:wasAttributedTo" do |object|
return nil if object.username.blank?

url = Rails.env.production? ? "https://api.datacite.org" : "https://api.test.datacite.org"
object.username.include?(".") ? url + "/clients/" + object.username : url + "/providers/" + object.username
if object.username.present?
url = Rails.env.production? ? "https://api.datacite.org" : "https://api.test.datacite.org"
object.username.include?(".") ? url + "/clients/" + object.username : url + "/providers/" + object.username
end
end

attribute "prov:wasGeneratedBy" do |object|
Expand Down
34 changes: 34 additions & 0 deletions spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "rails_helper"

describe Activity, type: :model do
context "create doi" do
let(:client) { create(:client) }
let(:doi) { create(:doi, client: client) }

it "activity exists" do
expect(doi.activities.length).to eq(1)
activity = doi.activities.first
expect(activity.uid).to eq(doi.uid)
# expect(activity.username).to eq(2)
expect(activity.request_uuid).to be_present
expect(activity.changes["aasm_state"]).to eq("draft")
expect(activity.changes["types"]).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceType"=>"DataPackage", "resourceTypeGeneral"=>"Dataset", "ris"=>"DATA", "schemaOrg"=>"Dataset")
end
end

context "update doi" do
let(:client) { create(:client) }
let(:doi) { create(:doi, client: client) }

it "activity exists" do
doi.update(event: "publish")

expect(doi.activities.length).to eq(2)
activity = doi.activities.last
expect(activity.uid).to eq(doi.uid)
# expect(activity.username).to eq(2)
expect(activity.request_uuid).to be_present
expect(activity.changes).to eq("aasm_state"=>["draft", "findable"])
end
end
end
34 changes: 34 additions & 0 deletions spec/requests/activities_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "rails_helper"

describe "activities", type: :request do
let(:provider) { create(:provider, symbol: "DATACITE") }
let(:client) { create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD']) }
let(:doi) { create(:doi, client: client) }
let(:bearer) { Client.generate_token(role_id: "client_admin", uid: client.symbol, provider_id: provider.symbol.downcase, client_id: client.symbol.downcase, password: client.password) }
let(:headers) { { "HTTP_ACCEPT"=>"application/vnd.api+json", "HTTP_AUTHORIZATION" => "Bearer " + bearer }}

describe "activities for doi", elasticsearch: true do
let!(:doi) { create(:doi, client: client) }

before do
Doi.import
Activity.import
sleep 1
end

context "without username" do
it "returns the activities" do
get "/activities", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("data", 0, "attributes", "action")).to eq("create")
expect(json.dig("data", 0, "attributes", "changes", "aasm_state")).to eq("draft")

expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil
expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present
expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present
expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present
end
end
end
end

0 comments on commit e7cb0c5

Please sign in to comment.