Skip to content

Commit

Permalink
Add and use decidim-bulletin_board gem (#6997)
Browse files Browse the repository at this point in the history
* add BulletinBoard gem

* set BulletinBoard configuration

* use BulletinBoard gem to setup an election

* remove graphlient gem

* remove bulletin board client

* fix scheme name

* remove graphlient require

* update setup election response handling

* add decidim-bulletin_board gem

* use decidim-bulletin_board gem

* fix public key factory

* lint code

* remove old Bulletin Board client

* update design_app gemfile

* update decidim generators gemfile

* update bulletin board docs

* fix bulletin board configuration

* fix: remove unneeded class (used from the BB gem)

* test: handle error messages during election setup

Co-authored-by: Leonardo Diez <leiodd@gmail.com>
  • Loading branch information
slickepinne and leio10 committed Dec 15, 2020
1 parent 2ced107 commit dd287eb
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 573 deletions.
9 changes: 8 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ PATH
webmock (~> 3.6)
wisper-rspec (~> 1.0)
decidim-elections (0.24.0.dev)
decidim-bulletin_board (= 0.2.0)
decidim-core (= 0.24.0.dev)
decidim-forms (= 0.24.0.dev)
decidim-proposals (= 0.24.0.dev)
graphlient (~> 0.4.0)
decidim-forms (0.24.0.dev)
decidim-core (= 0.24.0.dev)
wicked_pdf (~> 1.4)
Expand Down Expand Up @@ -354,6 +354,13 @@ GEM
db-query-matchers (0.9.0)
activesupport (>= 4.0, <= 6.0)
rspec (~> 3.0)
decidim-bulletin_board (0.2.0)
activemodel (~> 5.0, >= 5.0.0.1)
activesupport (~> 5.0, >= 5.0.0.1)
byebug (~> 11.0)
graphlient (~> 0.4.0)
jwt
wisper (~> 2.0.0)
declarative-builder (0.1.0)
declarative-option (< 0.2.0)
declarative-option (0.1.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def election_data
{
iat: Time.now.to_i,
election_id: election_id,
message_id: "#{election_id}.create_election+a.#{bulletin_board.authority_slug}",
type: "create_election",
scheme: bulletin_board.scheme,
trustees:
Expand Down Expand Up @@ -133,28 +134,14 @@ def election_data
end

def setup_election
signed_data = bulletin_board.encode_data(election_data)

response = bulletin_board.graphql_client.query do
mutation do
createElection(signedData: signed_data) do
election do
id
status
title
authority
end
error
end
end
end
response = bulletin_board.setup_election(election_data)

if response.data.create_election.error.present?
error = response.data.create_election.error
if response.error.present?
error = response.error
form.errors.add(:base, error)
raise ActiveRecord::Rollback
else
store_bulletin_board_status(response.data.create_election.election.status)
store_bulletin_board_status(response.election.status)
end
end

Expand Down
23 changes: 23 additions & 0 deletions decidim-elections/config/initializers/decidim_bulletin_board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

return if Rails.application.secrets.bulletin_board.blank?

Decidim::BulletinBoard.configure do |config|
# Exposes a configuration option: the bulletin board server
config.server = Rails.application.secrets.bulletin_board[:server]

# Exposes a configuration option: the api key generated by the Bulletin Board for the Decidim instance
config.api_key = Rails.application.secrets.bulletin_board[:api_key]

# Exposes a configuration option: the scheme to be used for messages
config.scheme = Rails.application.secrets.bulletin_board[:scheme]

# Exposes a configuration option: the authority name String
config.authority_name = Rails.application.secrets.bulletin_board[:authority_name]

# Exposes a configuration option: number of trustees for an election
config.number_of_trustees = Rails.application.secrets.bulletin_board[:number_of_trustees]

# Exposes a configuration option: private key, that got generated by the Decidim instance
config.identification_private_key = Rails.application.secrets.bulletin_board[:identification_private_key]
end
2 changes: 1 addition & 1 deletion decidim-elections/decidim-elections.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Gem::Specification.new do |s|

s.files = Dir["{app,config,db,lib}/**/*", "LICENSE-AGPLv3.txt", "Rakefile", "README.md"]

s.add_dependency "decidim-bulletin_board", "0.2.0"
s.add_dependency "decidim-core", Decidim::Elections.version
s.add_dependency "decidim-forms", Decidim::Elections.version
s.add_dependency "decidim-proposals", Decidim::Elections.version
s.add_dependency "graphlient", "~> 0.4.0"

s.add_development_dependency "decidim-admin", Decidim::Elections.version
s.add_development_dependency "decidim-dev", Decidim::Elections.version
Expand Down
5 changes: 2 additions & 3 deletions decidim-elections/lib/decidim/elections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
require "decidim/elections/admin_engine"
require "decidim/elections/trustee_zone_engine"
require "decidim/elections/component"
require "decidim/elections/jwk_utils"
require "decidim/bulletin_board"

module Decidim
# This namespace holds the logic of the `Elections` component. This component
# allows users to create elections in a participatory space.
module Elections
autoload :BulletinBoardClient, "decidim/elections/bulletin_board_client"
autoload :AnswerSerializer, "decidim/elections/answer_serializer"

def self.bulletin_board
@bulletin_board ||= BulletinBoardClient.new(Rails.application.secrets.bulletin_board || {})
@bulletin_board ||= Decidim::BulletinBoard::Client.new
end
end
end
57 changes: 0 additions & 57 deletions decidim-elections/lib/decidim/elections/bulletin_board_client.rb

This file was deleted.

1 change: 0 additions & 1 deletion decidim-elections/lib/decidim/elections/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "rails"
require "decidim/core"
require "graphlient"

module Decidim
module Elections
Expand Down
49 changes: 0 additions & 49 deletions decidim-elections/lib/decidim/elections/jwk_utils.rb

This file was deleted.

6 changes: 5 additions & 1 deletion decidim-elections/lib/decidim/elections/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@

trait :with_public_key do
considered
public_key { Random.urlsafe_base64(30) }
sequence(:public_key) do
private_key = JWT::JWK.new(OpenSSL::PKey::RSA.new(4096))
public_key = private_key.export
public_key.to_json
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion decidim-elections/lib/tasks/decidim_elections.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace :decidim_elections do

puts "\nPRIVATE KEY:"
puts "\n"
puts Decidim::Elections::JwkUtils.private_export(identification_jwk_keypair).to_json
puts Decidim::BulletinBoard::JwkUtils.private_export(identification_jwk_keypair).to_json
puts "\n"
puts "PUBLIC KEY:"
puts "\n"
Expand Down

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
subject { described_class.new(form, bulletin_board: bulletin_board) }

let(:organization) { create :organization, available_locales: [:en, :ca, :es], default_locale: :en }
let(:invalid) { false }
let(:participatory_process) { create :participatory_process, organization: organization }
let(:current_component) { create :component, participatory_space: participatory_process, manifest_name: "elections" }
let(:user) { create :user, :admin, :confirmed, organization: organization }
Expand All @@ -24,41 +25,30 @@
errors: errors
)
end
let(:bulletin_board) do
Decidim::Elections::BulletinBoardClient.new(
server: "http://localhost:8000/api",
api_key: Rails.application.secrets.bulletin_board[:api_key],
authority_name: "Decidim Test Authority",
scheme: {
name: "test",
parameters: {
quorum: 2
}
},
number_of_trustees: 2,
identification_private_key: identification_private_key
)
let(:scheme) do
{
name: "dummy",
parameters: {
quorum: 2
}
}
end

let(:identification_private_key) do
Rails.application.secrets.bulletin_board[:identification_private_key]
let(:status) { OpenStruct.new(status: "key_ceremony") }
let(:response) do
OpenStruct.new(election: status, error: nil)
end

let(:identification_private_key_content) do
Decidim::Elections::JwkUtils.import_private_key(identification_private_key)
let(:bulletin_board) do
double(Decidim::Elections.bulletin_board)
end

let(:invalid) { false }

context "when the form is not valid" do
let(:invalid) { true }

it "is not valid" do
expect { subject.call }.to broadcast(:invalid)
end
before do
allow(bulletin_board).to receive(:authority_slug).and_return("decidim-test-authority")
allow(bulletin_board).to receive(:scheme).and_return(scheme)
allow(bulletin_board).to receive(:setup_election).and_return(response)
end

context "when valid form", :vcr do
context "when valid form" do
let(:trustee_users) { trustees.collect(&:user) }

it "setup the election" do
Expand All @@ -76,4 +66,27 @@
expect(election.blocked?).to be true
end
end

context "when the form is not valid" do
let(:invalid) { true }

it "is not valid" do
expect { subject.call }.to broadcast(:invalid)
end
end

context "when the bulletin board returns an error message" do
let(:response) do
OpenStruct.new(election: nil, error: "An error!")
end

it "is not valid" do
expect { subject.call }.to broadcast(:invalid)
end

it "returns the error message" do
expect(form.errors).to receive(:add).with(:base, "An error!")
subject.call
end
end
end
Loading

0 comments on commit dd287eb

Please sign in to comment.