Every Certificate we create has a unique identifier and displays a url that can be used to verify the authenticity of the document.
In order for it to work we need to:
Create a route in our controller (application.rb)
Create appropriate templates for valid & invalid certificates
Query the database for a certificate using the identifier
Display the right certificate using the valid.erb template
Display the invalid.erb template if there is no matching certificate
We start with writing a Cucumber feature. Create a new feature file:
$ touch features/verify_certificate.feature
Add the following scenario to that feature file:
# features/verify_certificate.feature
Feature: As a certificate reviewer,
In order to asses the authenticity of a certificate
I want to be able to access a web page showing me the certificate information
by clicking the verification URL
Scenario: Verify certificate with a valid URL
Given valid certificates exists
And I visit the url for a certificate
Then I should be on the valid certificate page
Add the following step definitions to your application_steps.rb:
# features/support/application_steps.rbGiven(/^valid certificates exists$/) do steps %( Given the delivery for the course "Basic" is set to "2015-12-01" And the data file for "2015-12-01" is imported And I am on 2015-12-01 show page And I click "Generate certificates" link )endAnd(/^I visit the url for a certificate$/) do cert = Certificate.last visit "/verify/#{cert.identifier}"end
And the following path to your paths.rb:
# features/support/paths.rbwhen/the valid certificate page/ c = Certificate.last"/verify/#{c.identifier}"
Keep on running cucumber after each addition to see if the error message is changing. Create the following route to your application.rb
# lib/application.rb...# Verification URI get '/verify/:hash'do @certificate =Certificate.first(identifier: params[:hash])if @certificate @image ="/img/usr/#{env}/"+ [@certificate.student.full_name, @certificate.delivery.start_date].join('_').downcase.gsub!(/\s/,'_') +'.jpg' erb :'verify/valid'else erb :'verify/invalid'endend...
Now we need to create a subfolder to views called verify and create the two templates we are going to use. One for a valid certificate and one if the certificate identifier is invalid.
# lib/views/verify/invalid.erb
<h3 style="color: red">Invalid certificate</h3>
<p>If you are an employer and was presented with a copy of the certificate
as a part of an CV, know that this is a forged document </p>
<p>Please contact us with information on who gave it to you.</p>
Okay, run all your features and specs. Fire up the local server using rackup and have a look for yourself.
In order to get the verification url you'll need to access a generated pdf in your file system, open it and copy the link from the bottom of the page. Paste it in your browser and you should see the verification page.
Try modifying the long hash and reload the page. Now, you should see a page that uses the invalid.erb template.