From 9176ec238249c1518b3fe89c8c0002156503a05f Mon Sep 17 00:00:00 2001 From: Gary Krause Date: Wed, 1 May 2019 06:26:46 -0400 Subject: [PATCH 1/4] CodeSchool controller test expecting error when no HTTPS present in CodeSchool URL --- .gitignore | 2 ++ .../api/v1/code_schools_controller_test.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/.gitignore b/.gitignore index 0cfc50b4..5abfe1bf 100644 --- a/.gitignore +++ b/.gitignore @@ -302,3 +302,5 @@ dump.rdb # Model Graph image generated docs/models/model_graph.png +.vscode +OpCodeBackEnd.code-workspace diff --git a/test/controllers/api/v1/code_schools_controller_test.rb b/test/controllers/api/v1/code_schools_controller_test.rb index 0ddb16ca..acbdef2f 100644 --- a/test/controllers/api/v1/code_schools_controller_test.rb +++ b/test/controllers/api/v1/code_schools_controller_test.rb @@ -29,6 +29,22 @@ class Api::V1::CodeSchoolsControllerTest < ActionDispatch::IntegrationTest assert errors.include? "Url can't be blank" end + test ":validates CodeSchool uses HTTPS for URL" do + params = { + code_school: { + name: '1337School', + url: 'http://31337codeschool.com' + } + } + post api_v1_code_schools_url, + params: params, + headers: @headers, + as: :json + + errors = JSON.parse(response.body)['errors'] + assert errors.include? "Url must be https" + end + test ':index endpoint returns a JSON list of all CodeSchools' do get api_v1_code_schools_path, as: :json From a6a428f560653a709b43bd57cca62d84c919b5e9 Mon Sep 17 00:00:00 2001 From: Gary Krause Date: Fri, 3 May 2019 07:07:00 -0400 Subject: [PATCH 2/4] added URL validation to code_school. enforces https:// in the URL. however, locations_controller_test is broke now, suspect need to validate mock data --- app/models/code_school.rb | 3 ++- test/controllers/api/v1/code_schools_controller_test.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/code_school.rb b/app/models/code_school.rb index 9ffdff5e..ed9f92eb 100644 --- a/app/models/code_school.rb +++ b/app/models/code_school.rb @@ -1,5 +1,6 @@ class CodeSchool < ApplicationRecord - validates :name, :url, :logo, presence: true + validates :name, :logo, presence: true + validates :url, presence: true, format: { with: /\Ahttps:\/\/(.*)/i, message: "URL must be HTTPS, if unable please secure your site" } validates_inclusion_of :full_time, :hardware_included, :has_online, :online_only, :in => [true, false] has_many :locations, -> { order('state ASC, city ASC') }, dependent: :destroy end diff --git a/test/controllers/api/v1/code_schools_controller_test.rb b/test/controllers/api/v1/code_schools_controller_test.rb index acbdef2f..62029305 100644 --- a/test/controllers/api/v1/code_schools_controller_test.rb +++ b/test/controllers/api/v1/code_schools_controller_test.rb @@ -42,7 +42,7 @@ class Api::V1::CodeSchoolsControllerTest < ActionDispatch::IntegrationTest as: :json errors = JSON.parse(response.body)['errors'] - assert errors.include? "Url must be https" + assert errors.include? "URL must be HTTPS, if unable please secure your site" end test ':index endpoint returns a JSON list of all CodeSchools' do From d5c8e0033e7fefd39398af828e901def6bc3a2d4 Mon Sep 17 00:00:00 2001 From: Gary Krause Date: Fri, 3 May 2019 20:37:35 -0400 Subject: [PATCH 3/4] Baked in syntax for running verbose tests and single tests --- Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1611d380..91b0e1c8 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,12 @@ db_seed: .PHONY: test test: bg docker-compose run operationcode-psql bash -c "while ! psql --host=operationcode-psql --username=postgres -c 'SELECT 1'; do sleep 5; done;" - docker-compose run ${RAILS_CONTAINER} bash -c 'export RAILS_ENV=test && bundle exec rake db:test:prepare && bundle exec rake test && bundle exec rubocop' + docker-compose run ${RAILS_CONTAINER} bash -xc 'export RAILS_ENV=test && bundle exec rake db:test:prepare && bundle exec rake test && bundle exec rubocop' + # change the appropriate portion of above to enable verbose test output + # bundle exec rake test TESTOPTS='-v' + # + # This one allows you to run one test file at a time by changing the file path to a specific test file + # bundle exec rake test TEST=test/controllers/api/v1/code_schools_controller_test.rb TESTOPTS='-v' .PHONY: rubocop rubocop: From f94dd8d24a5ab950cca67264cf871a2e6659f880 Mon Sep 17 00:00:00 2001 From: Gary Krause Date: Fri, 3 May 2019 20:40:16 -0400 Subject: [PATCH 4/4] Added HTTPS validation for code school URL --- app/models/code_school.rb | 4 ++-- test/controllers/api/v1/code_schools_controller_test.rb | 4 ++-- test/factories/code_schools.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/code_school.rb b/app/models/code_school.rb index ed9f92eb..8b048fbb 100644 --- a/app/models/code_school.rb +++ b/app/models/code_school.rb @@ -1,6 +1,6 @@ class CodeSchool < ApplicationRecord - validates :name, :logo, presence: true - validates :url, presence: true, format: { with: /\Ahttps:\/\/(.*)/i, message: "URL must be HTTPS, if unable please secure your site" } + validates :name, :logo, :url, presence: true + validates :url, presence: true, format: { with: %r{\Ahttps://(.*)}, message: 'must be HTTPS, if unable please secure your site' } validates_inclusion_of :full_time, :hardware_included, :has_online, :online_only, :in => [true, false] has_many :locations, -> { order('state ASC, city ASC') }, dependent: :destroy end diff --git a/test/controllers/api/v1/code_schools_controller_test.rb b/test/controllers/api/v1/code_schools_controller_test.rb index 62029305..b592ef09 100644 --- a/test/controllers/api/v1/code_schools_controller_test.rb +++ b/test/controllers/api/v1/code_schools_controller_test.rb @@ -29,7 +29,7 @@ class Api::V1::CodeSchoolsControllerTest < ActionDispatch::IntegrationTest assert errors.include? "Url can't be blank" end - test ":validates CodeSchool uses HTTPS for URL" do + test ':validates CodeSchool uses HTTPS for URL' do params = { code_school: { name: '1337School', @@ -42,7 +42,7 @@ class Api::V1::CodeSchoolsControllerTest < ActionDispatch::IntegrationTest as: :json errors = JSON.parse(response.body)['errors'] - assert errors.include? "URL must be HTTPS, if unable please secure your site" + assert errors.include? 'Url must be HTTPS, if unable please secure your site' end test ':index endpoint returns a JSON list of all CodeSchools' do diff --git a/test/factories/code_schools.rb b/test/factories/code_schools.rb index b7e934e9..b1150116 100644 --- a/test/factories/code_schools.rb +++ b/test/factories/code_schools.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :code_school do name { Faker::Company.name } - url { Faker::Internet.url } + url { Faker::Internet.url(Faker::Internet.domain_name, '', 'https') } logo { Faker::Internet.url } full_time { Faker::Boolean.boolean } hardware_included { Faker::Boolean.boolean }