Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ND-231 Add UPVS submission tests #600

Merged
merged 12 commits into from
Sep 18, 2023
7 changes: 4 additions & 3 deletions .github/workflows/slovensko_digital_ci.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
name: Slovensko.Digital CI

on:
push:
on:
push:
branches: master
pull_request:
branches: '**'

jobs:
test:
runs-on: ubuntu-latest

env:
PGHOST: localhost
RAILS_ENV: test
SLOVENSKO_SK_API_URL: https://test.slovensko.digital

services:
postgres:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'hirb'

gem "rails-controller-testing", "~> 1.0"
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ GEM
bundler (>= 1.15.0)
railties (= 6.1.7.3)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand Down Expand Up @@ -490,6 +494,7 @@ DEPENDENCIES
que
que-web
rails (~> 6.1.7.3)
rails-controller-testing (~> 1.0)
rails-i18n
recaptcha
rollbar
Expand Down
34 changes: 4 additions & 30 deletions app/controllers/upvs/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,18 @@ def switch_account_callback
def submit
return switch_account_callback unless eid_token&.valid? # If token expires meanwhile

if @upvs_submission.valid?
response = submit_to_sk_api

if successful_sk_api_submission?(response)
if @upvs_submission.callback_url.present?
redirect_to @upvs_submission.callback_url
else
redirect_to action: :finish
end
if @upvs_submission.submit(eid_token)
if @upvs_submission.callback_url.present?
redirect_to @upvs_submission.callback_url
else
raise Upvs::Submission::SkApiError.new
redirect_to action: :finish
end
else
render :new, status: :unprocessable_entity
end
end

def finish
@upvs_submission.update(expires_at: Time.zone.now)
end

def submission_error
Expand All @@ -76,25 +69,6 @@ def set_metadata
@metadata.og.title = params[:title] || 'Návody.Digital: Podanie' # TODO
end

def submit_to_sk_api(client: Faraday)
begin
headers = { "Content-Type": "application/json" }
data = { message: UpvsSubmissions::SktalkMessageBuilder.new.build_sktalk_message(@upvs_submission, eid_token) }.to_json
url = "#{ENV.fetch('SLOVENSKO_SK_API_URL')}/api/sktalk/receive_and_save_to_outbox?token=#{eid_token&.api_token}"

client.post(url, data, headers)
rescue Exception
raise Upvs::Submission::SkApiError.new
end
end

def successful_sk_api_submission?(response)
json_body = JSON.parse(response.body)

return true if (response.status == 200 && json_body["receive_result"] == 0 && json_body["save_to_outbox_result"] == 0)
false
end

def build_upvs_submission
@upvs_submission = current_user.build_upvs_submission(
submission_params,
Expand Down
45 changes: 40 additions & 5 deletions app/models/upvs/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class Upvs::Submission < ApplicationRecord
before_create { self.uuid = SecureRandom.uuid } # TODO ensure unique in loop
before_create { set_new_expiration_time }

validates_presence_of :posp_id, :posp_version, :message_type, :recipient_uri, :message_subject, :form
validate :recipient_uri_allowed?, if: -> { Rails.env.production? }
validate :egov_application_allowed?, if: -> { Rails.env.production? }
validate :valid_xml_form?
validates_presence_of :posp_id, :posp_version, :message_type, :recipient_uri, :message_subject, :form, on: :create
validate :recipient_uri_allowed?, if: -> { Rails.env.production? }, on: :create
validate :egov_application_allowed?, if: -> { Rails.env.production? }, on: :create
validate :valid_xml_form?, on: :create

scope :expired, -> { where('expires_at < ?', Time.zone.now) }

Expand Down Expand Up @@ -68,8 +68,43 @@ def to_param
uuid
end

def submit(eid_token, client: Faraday, url: "#{ENV.fetch('SLOVENSKO_SK_API_URL')}/api/sktalk/receive_and_save_to_outbox?token=#{eid_token&.api_token}")
return false unless valid?

response = submit_to_sk_api(client, url, eid_token)

if successful_sk_api_submission?(response)
update(expires_at: Time.zone.now)
return true
else
raise Upvs::Submission::SkApiError.new
end
end

private

def validate
false unless valid?
end

def submit_to_sk_api(client, url, eid_token)
begin
headers = { "Content-Type": "application/json" }
data = { message: UpvsSubmissions::SktalkMessageBuilder.new.build_sktalk_message(self, eid_token) }.to_json

client.post(url, data, headers)
rescue Exception
raise Upvs::Submission::SkApiError.new
end
end

def successful_sk_api_submission?(response)
json_body = JSON.parse(response.body)

return true if (response.status == 200 && json_body["receive_result"] == 0 && json_body["save_to_outbox_result"] == 0)
false
end

def set_new_expiration_time
self.expires_at = Time.zone.now + 20.minutes
end
Expand Down Expand Up @@ -97,7 +132,7 @@ def recipient_uri_allowed?
end

def valid_xml_form?
unless Nokogiri::XML(@form).errors.empty?
unless Nokogiri::XML(form).errors.empty?
errors.add(:form, "Nevalidný XML formulár")
end
end
Expand Down
1 change: 0 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ test:
datahub:
<<: *default
database: datahub_test
replica: true

staging:
primary:
Expand Down
169 changes: 169 additions & 0 deletions db/datahub_structure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: upvs; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA upvs;


SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: public_authority_edesks; Type: TABLE; Schema: upvs; Owner: -
--

CREATE TABLE upvs.public_authority_edesks (
id integer NOT NULL,
cin bigint NOT NULL,
uri character varying NOT NULL,
name character varying NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);


--
-- Name: public_authority_edesks_id_seq; Type: SEQUENCE; Schema: upvs; Owner: -
--

CREATE SEQUENCE upvs.public_authority_edesks_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: public_authority_edesks_id_seq; Type: SEQUENCE OWNED BY; Schema: upvs; Owner: -
--

ALTER SEQUENCE upvs.public_authority_edesks_id_seq OWNED BY upvs.public_authority_edesks.id;


--
-- Name: services_with_forms; Type: TABLE; Schema: upvs; Owner: -
--

CREATE TABLE upvs.services_with_forms (
id integer NOT NULL,
instance_id integer NOT NULL,
external_code character varying,
meta_is_code character varying,
name character varying,
type character varying,
institution_uri character varying NOT NULL,
institution_name character varying,
valid_from timestamp without time zone,
valid_to timestamp without time zone,
url character varying,
info_url character varying,
schema_url character varying,
changed_at timestamp without time zone,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);


--
-- Name: services_with_forms_id_seq; Type: SEQUENCE; Schema: upvs; Owner: -
--

CREATE SEQUENCE upvs.services_with_forms_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: services_with_forms_id_seq; Type: SEQUENCE OWNED BY; Schema: upvs; Owner: -
--

ALTER SEQUENCE upvs.services_with_forms_id_seq OWNED BY upvs.services_with_forms.id;

--
-- Name: public_authority_edesks id; Type: DEFAULT; Schema: upvs; Owner: -
--

ALTER TABLE ONLY upvs.public_authority_edesks ALTER COLUMN id SET DEFAULT nextval('upvs.public_authority_edesks_id_seq'::regclass);


--
-- Name: services_with_forms id; Type: DEFAULT; Schema: upvs; Owner: -
--

ALTER TABLE ONLY upvs.services_with_forms ALTER COLUMN id SET DEFAULT nextval('upvs.services_with_forms_id_seq'::regclass);


--
-- Name: public_authority_edesks public_authority_edesks_pkey; Type: CONSTRAINT; Schema: upvs; Owner: -
--

ALTER TABLE ONLY upvs.public_authority_edesks
ADD CONSTRAINT public_authority_edesks_pkey PRIMARY KEY (id);


--
-- Name: services_with_forms services_with_forms_pkey; Type: CONSTRAINT; Schema: upvs; Owner: -
--

ALTER TABLE ONLY upvs.services_with_forms
ADD CONSTRAINT services_with_forms_pkey PRIMARY KEY (id);


--
-- Name: index_upvs.public_authority_edesks_on_cin; Type: INDEX; Schema: upvs; Owner: -
--

CREATE INDEX "index_upvs.public_authority_edesks_on_cin" ON upvs.public_authority_edesks USING btree (cin);


--
-- Name: index_upvs.public_authority_edesks_on_uri; Type: INDEX; Schema: upvs; Owner: -
--

CREATE UNIQUE INDEX "index_upvs.public_authority_edesks_on_uri" ON upvs.public_authority_edesks USING btree (uri);


--
-- Name: index_upvs.services_with_forms_on_instance_id; Type: INDEX; Schema: upvs; Owner: -
--

CREATE UNIQUE INDEX "index_upvs.services_with_forms_on_instance_id" ON upvs.services_with_forms USING btree (instance_id);


--
-- Name: index_upvs.services_with_forms_on_institution_uri; Type: INDEX; Schema: upvs; Owner: -
--

CREATE INDEX "index_upvs.services_with_forms_on_institution_uri" ON upvs.services_with_forms USING btree (institution_uri);


--
-- Name: index_upvs.services_with_forms_on_schema_url; Type: INDEX; Schema: upvs; Owner: -
--

CREATE INDEX "index_upvs.services_with_forms_on_schema_url" ON upvs.services_with_forms USING btree (schema_url);


--
-- PostgreSQL database dump complete
--

SET search_path TO "$user", public;
Loading