-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: connect gitcoin score strategy to action execution flow
- Loading branch information
1 parent
eb71a4e
commit 87af4af
Showing
16 changed files
with
273 additions
and
158 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
85 changes: 85 additions & 0 deletions
85
...vquests-api/govquests/action_tracking/test/strategy/gitcoin_score_action_strategy_test.rb
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,85 @@ | ||
# govquests/action_tracking/test/strategy/gitcoin_score_action_strategy_test.rb | ||
|
||
require_relative "../test_helper" | ||
require "minitest/mock" | ||
|
||
module ActionTracking | ||
class GitcoinScoreActionStrategyTest < Test | ||
def setup | ||
super | ||
@strategy = GitcoinScoreActionStrategy.new | ||
@api_mock = Minitest::Mock.new | ||
@strategy.instance_variable_set(:@api, @api_mock) | ||
end | ||
|
||
def test_action_type | ||
assert_equal "gitcoin_score", @strategy.send(:action_type) | ||
end | ||
|
||
def test_description | ||
assert_equal "Complete Gitcoin Passport verification", @strategy.send(:description) | ||
end | ||
|
||
def test_action_data | ||
assert_equal({min_score: 20}, @strategy.send(:action_data, {})) | ||
assert_equal({min_score: 30}, @strategy.send(:action_data, {min_score: 30})) | ||
end | ||
|
||
def test_start_execution | ||
@api_mock.expect :get_signing_message, {"nonce" => "test_nonce", "message" => "test_message"} | ||
|
||
result = @strategy.start_execution({}) | ||
|
||
assert_equal({ | ||
state: "message_requested", | ||
nonce: "test_nonce", | ||
message: "test_message" | ||
}, result) | ||
|
||
@api_mock.verify | ||
end | ||
|
||
def test_complete_execution_success | ||
@api_mock.expect :submit_passport, {"score" => 25}, ["0x123", "signature", "nonce"] | ||
|
||
result = @strategy.complete_execution({ | ||
address: "0x123", | ||
signature: "signature", | ||
nonce: "nonce" | ||
}) | ||
|
||
assert_equal({ | ||
status: "completed", | ||
state: "done", | ||
score: 25, | ||
message: "Gitcoin Passport verified successfully" | ||
}, result) | ||
|
||
@api_mock.verify | ||
end | ||
|
||
def test_complete_execution_failure | ||
@api_mock.expect :submit_passport, {"score" => 15}, ["0x123", "signature", "nonce"] | ||
|
||
result = @strategy.complete_execution({ | ||
address: "0x123", | ||
signature: "signature", | ||
nonce: "nonce" | ||
}) | ||
|
||
assert_equal({ | ||
status: "failed", | ||
state: "done", | ||
score: 15, | ||
message: "Gitcoin Passport score too low" | ||
}, result) | ||
|
||
@api_mock.verify | ||
end | ||
|
||
def test_verify_completion | ||
assert @strategy.send(:verify_completion, {score: 25}) | ||
refute @strategy.send(:verify_completion, {score: 15}) | ||
end | ||
end | ||
end |
31 changes: 30 additions & 1 deletion
31
apps/govquests-api/rails_app/app/blueprints/action_execution_blueprint.rb
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 |
---|---|---|
@@ -1,3 +1,32 @@ | ||
# { | ||
# "data": { | ||
# "id": 2, | ||
# "execution_id": "46b0fe20-dabc-45dd-bfc1-5e0f56d627e2", | ||
# "action_id": "54d24d70-1c89-4a0a-88ad-e24a1926a2f3", | ||
# "user_id": "1da1cdab-a037-4d92-8625-717c5fad9f35", | ||
# "started_at": "2024-10-07T19:11:40.356Z", | ||
# "status": "started", | ||
# "created_at": "2024-10-07T19:11:40.402Z", | ||
# "updated_at": "2024-10-07T19:11:40.402Z", | ||
# "action_type": "gitcoin_score", | ||
# "result": null, | ||
# "completed_at": null, | ||
# "nonce": "20f961d38281f14381a06dc9b69080f7", | ||
# "start_data": { | ||
# "step": 0, | ||
# "nonce": "e8274b89c180a97dfb7268468484410b82572b0690fefd309d3131d8ab03", | ||
# "state": "started", | ||
# "message": "I hereby agree to submit my address in order to score my associated Gitcoin Passport from Ceramic.\n\nNonce: e8274b89c180a97dfb7268468484410b82572b0690fefd309d3131d8ab03\n", | ||
# "stepCount": 1 | ||
# }, | ||
# "completion_data": {} | ||
# }, | ||
# "nonce": "20f961d38281f14381a06dc9b69080f7", | ||
# "execution_id": "46b0fe20-dabc-45dd-bfc1-5e0f56d627e2", | ||
# "expires_at": "2024-10-07T19:41:40.356Z" | ||
# } | ||
class ActionExecutionBlueprint < Blueprinter::Base | ||
fields :token, :execution_id, :expires_at | ||
fields :execution_id, :start_data, :nonce, :completion_data, :action_type | ||
|
||
identifier :execution_id | ||
end |
20 changes: 10 additions & 10 deletions
20
apps/govquests-api/rails_app/app/controllers/action_executions_controller.rb
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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
class ActionExecutionsController < ApplicationController | ||
def start | ||
result = ActionTracking::ActionExecutionService.start( | ||
action_execution = ActionTracking::ActionExecutionService.start( | ||
action_id: params[:action_id], | ||
# TODO: user_id will be removed once we derive the user from the session | ||
user_id: params[:user_id], | ||
user_id: current_user.user_id, | ||
start_data: params[:start_data]&.to_unsafe_h || {} | ||
) | ||
|
||
if result[:error] | ||
render json: {error: result[:error]}, status: :not_found | ||
if action_execution[:error] | ||
render json: {error: action_execution[:error]}, status: :not_found | ||
else | ||
render json: result | ||
render json: ActionExecutionBlueprint.render(action_execution) | ||
end | ||
end | ||
|
||
def complete | ||
result = ActionTracking::ActionExecutionService.complete( | ||
action_execution = ActionTracking::ActionExecutionService.complete( | ||
execution_id: params[:execution_id], | ||
nonce: params[:nonce], | ||
# TODO: user_id will be removed once we derive the user from the session | ||
user_id: params[:user_id], | ||
user_id: current_user.user_id, | ||
completion_data: params[:completion_data]&.to_unsafe_h || {} | ||
) | ||
|
||
if result[:error] | ||
render json: {error: result[:error]}, status: :unprocessable_entity | ||
if action_execution[:error] | ||
render json: {error: action_execution[:error]}, status: :unprocessable_entity | ||
else | ||
render json: result | ||
render json: ActionExecutionBlueprint.render(action_execution) | ||
end | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
apps/govquests-api/rails_app/app/controllers/application_controller.rb
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class ApplicationController < ActionController::API | ||
def current_user | ||
@current_user ||= Authentication::UserReadModel.first | ||
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
Oops, something went wrong.