-
Notifications
You must be signed in to change notification settings - Fork 5
Store school email domains #787
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
Open
PetarSimonovic
wants to merge
10
commits into
main
Choose a base branch
from
store-school-email-domains
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8261e61
Create school_email_domains table
PetarSimonovic 49bb35e
Add SchoolEmailDomain model
PetarSimonovic 8391fbb
Format domains
PetarSimonovic aff003a
Add has_many association on School
PetarSimonovic 6417af7
Merge branch 'main' into store-school-email-domains
PetarSimonovic b528185
Scope domain uniqueness to school_id
PetarSimonovic cb94318
Use type of error instead of string
PetarSimonovic ce7c580
Declare subject above other let statements
PetarSimonovic 142142c
Parse and return host when URI provided
PetarSimonovic 80f2296
Validate domains against Public Suffix List
PetarSimonovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SchoolEmailDomain < ApplicationRecord | ||
| belongs_to :school | ||
|
|
||
| validates :domain, presence: true | ||
| validates :domain, uniqueness: { scope: :school_id } | ||
|
|
||
| before_validation :normalise_domain | ||
| validate :validate_public_suffix | ||
|
|
||
| private | ||
|
|
||
| def normalise_domain | ||
| return if domain.nil? | ||
|
|
||
| self.domain = build_normalised_domain_string(domain) | ||
| end | ||
|
|
||
| # Uses the Public Suffix List via the public_suffix gem: values must be a real | ||
| # hostname with a registrable name, not a bare suffix like com or co.uk. | ||
| # https://publicsuffix.org | ||
| def validate_public_suffix | ||
| return if domain.blank? | ||
|
|
||
| errors.add(:domain, :invalid) unless PublicSuffix.valid?(domain) | ||
| end | ||
|
|
||
| def build_normalised_domain_string(raw) | ||
| str = raw.to_s.strip.sub(/\A@+/, '') | ||
| str = uri_host_if_http_url(str) || str | ||
| str.downcase | ||
| end | ||
|
|
||
| def uri_host_if_http_url(str) | ||
| return unless str.match?(%r{\Ahttps?://}i) | ||
|
|
||
| uri = URI.parse(str) | ||
| uri.host if uri.is_a?(URI::HTTP) && uri.host.present? | ||
| rescue URI::InvalidURIError | ||
| nil | ||
| end | ||
|
PetarSimonovic marked this conversation as resolved.
|
||
| end | ||
This file contains hidden or 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,14 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CreateSchoolEmailDomains < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :school_email_domains, id: :uuid do |t| | ||
|
PetarSimonovic marked this conversation as resolved.
|
||
| t.references :school, null: false, foreign_key: true, type: :uuid | ||
| t.string :domain, null: false | ||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| add_index :school_email_domains, %i[school_id domain], unique: true | ||
| end | ||
| end | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,110 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe SchoolEmailDomain do | ||
|
PetarSimonovic marked this conversation as resolved.
|
||
| subject { described_class.new(school:, domain: 'example.edu') } | ||
|
|
||
| let(:school) { create(:school, creator_id: SecureRandom.uuid) } | ||
|
|
||
| it { is_expected.to belong_to(:school) } | ||
| it { is_expected.to validate_presence_of(:domain) } | ||
|
|
||
|
PetarSimonovic marked this conversation as resolved.
|
||
| describe 'domain normalisation' do | ||
| it 'takes the host from an http URL before other normalisation' do | ||
| record = described_class.new(school:, domain: 'http://mail.school.edu/path?query=1') | ||
| record.valid? | ||
|
|
||
| expect(record.domain).to eq('mail.school.edu') | ||
| end | ||
|
|
||
| it 'takes the host from an https URL before other normalisation' do | ||
| record = described_class.new(school:, domain: 'https://EXAMPLE.EDU/') | ||
| record.valid? | ||
|
|
||
| expect(record.domain).to eq('example.edu') | ||
| end | ||
|
|
||
| it 'downcases the domain' do | ||
| record = described_class.new(school:, domain: 'EXAMPLE.EDU') | ||
| record.valid? | ||
|
|
||
| expect(record.domain).to eq('example.edu') | ||
| end | ||
|
|
||
| it 'removes a leading @' do | ||
| record = described_class.new(school:, domain: '@example.edu') | ||
| record.valid? | ||
|
|
||
| expect(record.domain).to eq('example.edu') | ||
| end | ||
|
PetarSimonovic marked this conversation as resolved.
|
||
| end | ||
|
|
||
| describe 'public suffix list validation' do | ||
| context 'when the hostname is only a suffix' do | ||
| it 'rejects com' do | ||
| record = described_class.new(school:, domain: 'com') | ||
| record.valid? | ||
|
|
||
| expect(record).not_to be_valid | ||
| expect(record.errors.of_kind?(:domain, :invalid)).to be(true) | ||
| end | ||
|
|
||
| it 'rejects edu' do | ||
| record = described_class.new(school:, domain: 'edu') | ||
| record.valid? | ||
|
|
||
| expect(record).not_to be_valid | ||
| expect(record.errors.of_kind?(:domain, :invalid)).to be(true) | ||
| end | ||
|
|
||
| it 'rejects co.uk' do | ||
| record = described_class.new(school:, domain: 'co.uk') | ||
| record.valid? | ||
|
|
||
| expect(record).not_to be_valid | ||
| expect(record.errors.of_kind?(:domain, :invalid)).to be(true) | ||
| end | ||
| end | ||
|
|
||
| context 'when there is at least one registrable label before the public suffix' do | ||
| it 'accepts a typical school-style .edu domain' do | ||
| record = described_class.new(school:, domain: 'example.edu') | ||
| expect(record).to be_valid | ||
| end | ||
|
|
||
| it 'accepts a subdomain' do | ||
| record = described_class.new(school:, domain: 'mail.example.edu') | ||
| expect(record).to be_valid | ||
| end | ||
|
|
||
| it 'accepts a hostname under a multi-part public suffix' do | ||
| record = described_class.new(school:, domain: 'school.example.co.uk') | ||
| expect(record).to be_valid | ||
| end | ||
|
|
||
| it 'accepts district-style hosts' do | ||
| record = described_class.new(school:, domain: 'school.k12.tx.us') | ||
| expect(record).to be_valid | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'domain uniqueness' do | ||
| it 'rejects a duplicate domain for the same school after normalisation' do | ||
| described_class.create!(school:, domain: 'example.edu') | ||
| duplicate = described_class.new(school:, domain: 'EXAMPLE.EDU') | ||
| duplicate.valid? | ||
|
|
||
| expect(duplicate.errors.of_kind?(:domain, :taken)).to be(true) | ||
| end | ||
|
|
||
| it 'allows the same domain for a different school' do | ||
| described_class.create!(school:, domain: 'example.edu') | ||
| other_school = create(:school, creator_id: SecureRandom.uuid) | ||
| other = described_class.new(school: other_school, domain: 'example.edu') | ||
|
|
||
| expect(other).to be_valid | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.