-
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
13
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
13 commits
Select commit
Hold shift + click to select a range
1271659
Create school_email_domains table
PetarSimonovic 89183cf
Add SchoolEmailDomain model
PetarSimonovic ec46038
Format domains
PetarSimonovic d55bdf4
Add has_many association on School
PetarSimonovic 25e5d20
Scope domain uniqueness to school_id
PetarSimonovic 7ece122
Use type of error instead of string
PetarSimonovic 92c8194
Declare subject above other let statements
PetarSimonovic 645db9a
Parse and return host when URI provided
PetarSimonovic 719a809
Validate domains against Public Suffix List
PetarSimonovic 70c6174
Update validation and specs
PetarSimonovic 05bf918
Add email domain validation on School
PetarSimonovic f5eb365
Align host validation with Profile
PetarSimonovic 8b96928
Add SchoolEmailDomainValidator
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
Some comments aren't visible on the classic Files Changed page.
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,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SchoolEmailDomain < ApplicationRecord | ||
| belongs_to :school | ||
|
|
||
| validates :domain, presence: true | ||
|
PetarSimonovic marked this conversation as resolved.
|
||
| validates :domain, uniqueness: { scope: :school_id } | ||
|
|
||
| before_validation :validate_domain | ||
|
|
||
| private | ||
|
|
||
| def validate_domain | ||
| self.domain = SchoolEmailDomainValidator.call(domain) | ||
| rescue ::SchoolEmailDomainValidator::Error => e | ||
| errors.add(:domain, e.error_code) | ||
| 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,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SchoolEmailDomainValidator | ||
| class Error < StandardError | ||
| def error_code | ||
| :invalid | ||
| end | ||
| end | ||
|
|
||
| class BlankDomainError < Error | ||
| def error_code | ||
| :blank | ||
| end | ||
| end | ||
|
|
||
| class InvalidURIError < Error | ||
| def error_code | ||
| :invalid_uri | ||
| end | ||
| end | ||
|
|
||
| class InvalidHostError < Error | ||
| def error_code | ||
| :invalid_host | ||
| end | ||
| end | ||
|
|
||
| class PublicSuffixError < Error | ||
| def error_code | ||
| :invalid_public_suffix | ||
| end | ||
| end | ||
|
|
||
| def self.call(domain) | ||
| raise BlankDomainError if domain.blank? | ||
|
|
||
| validate_domain(domain) | ||
| end | ||
|
|
||
| def self.validate_domain(domain) | ||
| value = domain.strip.downcase | ||
| # Add a scheme unless it already has one, so URI can parse it | ||
| value = "http://#{value}" unless %r{\A[a-z][a-z0-9+\-.]*://}i.match?(value) | ||
| uri = URI.parse(value) | ||
| host = uri.host&.delete_suffix('.') | ||
|
|
||
| validate_host(host) | ||
| rescue URI::InvalidURIError | ||
| raise InvalidURIError | ||
| end | ||
|
|
||
| def self.validate_host(host) | ||
| accounts_host_format = | ||
| /\A\s*(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}\s*\z/i | ||
|
|
||
| raise InvalidHostError unless host&.match?(accounts_host_format) | ||
|
|
||
| raise PublicSuffixError, 'domain has no registered public suffix' unless PublicSuffix.valid?(host) | ||
|
|
||
| host | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe SchoolEmailDomainValidator do | ||
| describe '.call' do | ||
| it 'returns success with a normalised host for a valid domain' do | ||
| result = described_class.call('example.com') | ||
|
|
||
| expect(result).to eq('example.com') | ||
| end | ||
|
|
||
| context 'with a valid domain' do | ||
| it 'extracts the host for http' do | ||
| result = described_class.call('http://mail.school.edu/path?query=1') | ||
| expect(result).to eq('mail.school.edu') | ||
| end | ||
|
|
||
| it 'extracts the host for https' do | ||
| result = described_class.call('https://mail.school.edu/path?query=1') | ||
| expect(result).to eq('mail.school.edu') | ||
| end | ||
|
|
||
| it 'removes a trailing dot' do | ||
| result = described_class.call('example.edu.') | ||
| expect(result).to eq('example.edu') | ||
| end | ||
|
|
||
| it 'lowercases the host' do | ||
| result = described_class.call('EXAMPLE.EDU') | ||
| expect(result).to eq('example.edu') | ||
| end | ||
|
|
||
| it 'removes a leading @' do | ||
| result = described_class.call('@example.edu') | ||
| expect(result).to eq('example.edu') | ||
| end | ||
|
|
||
| it 'accepts a subdomain' do | ||
| result = described_class.call('mail.example.edu') | ||
| expect(result).to eq('mail.example.edu') | ||
| end | ||
|
|
||
| it 'accepts a hostname er a multi-part public suffix' do | ||
| result = described_class.call('school.example.co.uk') | ||
| expect(result).to eq('school.example.co.uk') | ||
| end | ||
|
|
||
| it 'accepts a district-style with a multi-part public suffix' do | ||
| result = described_class.call('school.k12.tx.us') | ||
| expect(result).to eq('school.k12.tx.us') | ||
| end | ||
| end | ||
|
|
||
| context 'when the domain is blank' do | ||
| it 'raises BlankDomain' do | ||
| expect { described_class.call('') }.to raise_error(SchoolEmailDomainValidator::BlankDomainError) | ||
| end | ||
| end | ||
|
|
||
| context 'when the domain has an invalid URI' do | ||
| it 'raises URI::InvalidURIError' do | ||
| expect { described_class.call('https://exa mple.com') }.to raise_error(SchoolEmailDomainValidator::InvalidURIError) | ||
| end | ||
| end | ||
|
|
||
| context 'when the host does not match accounts_host_format' do | ||
| it 'raises InvalidHostError' do | ||
| expect { described_class.call('school_domain.edu') } | ||
| .to raise_error(SchoolEmailDomainValidator::InvalidHostError) | ||
| end | ||
| end | ||
|
|
||
| context 'when the host fails PublicSuffix validation' do | ||
| it 'raises PublicSuffixError' do | ||
| expect { described_class.call('co.uk') } | ||
| .to raise_error(SchoolEmailDomainValidator::PublicSuffixError) | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe SchoolEmailDomain do | ||
|
PetarSimonovic marked this conversation as resolved.
|
||
| subject(:school_email_domain) { described_class.create!(school:, domain:) } | ||
|
|
||
| let(:school) { create(:school, creator_id: SecureRandom.uuid) } | ||
| let(:domain) { 'example.edu' } | ||
|
|
||
| describe 'associations' do | ||
| it { is_expected.to belong_to(:school) } | ||
| it { is_expected.to validate_presence_of(:domain) } | ||
| it { is_expected.to be_valid } | ||
| end | ||
|
|
||
| context 'with a valid domain' do | ||
| it 'accepts the domain' do | ||
| expect(school_email_domain.domain).to eq('example.edu') | ||
| end | ||
|
|
||
| describe 'domain normalisation' do | ||
| let(:domain) { '@EXAMPLE.EDU.' } | ||
|
|
||
| it 'normalises a domain' do | ||
| expect(school_email_domain.domain).to eq('example.edu') | ||
| end | ||
| end | ||
|
|
||
| describe 'domain uniqueness' do | ||
| context 'when the proposed domain matches the existing record' do | ||
| subject(:school_email_domain) { described_class.new(school:, domain:) } | ||
|
|
||
| let(:domain) { 'example.edu' } | ||
|
|
||
| before do | ||
| described_class.create!(school:, domain: 'example.edu') | ||
| school_email_domain.valid? | ||
| end | ||
|
|
||
| it 'rejects the duplicate' do | ||
| expect(school_email_domain).not_to be_valid | ||
| end | ||
|
|
||
| it 'records :taken on domain' do | ||
| expect(school_email_domain.errors.of_kind?(:domain, :taken)).to be(true) | ||
| end | ||
| end | ||
|
|
||
| context 'when the proposed domain matches after normalisation' do | ||
| subject(:school_email_domain) { described_class.new(school:, domain:) } | ||
|
|
||
| let(:domain) { 'http://EXAMPLE.EDU' } | ||
|
|
||
| before do | ||
| described_class.create!(school:, domain: 'example.edu') | ||
| school_email_domain.valid? | ||
| end | ||
|
|
||
| it 'rejects the duplicate' do | ||
| expect(school_email_domain).not_to be_valid | ||
| end | ||
|
|
||
| it 'records :taken on domain' do | ||
| expect(school_email_domain.errors.of_kind?(:domain, :taken)).to be(true) | ||
| end | ||
| 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_school_email_domain = described_class.new(school: other_school, domain: 'example.edu') | ||
|
|
||
| expect(other_school_email_domain).to be_valid | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'with an invalid domain' do | ||
| it { is_expected.not_to allow_value('').for(:domain) } | ||
| it { is_expected.not_to allow_value(' ').for(:domain) } | ||
| it { is_expected.not_to allow_value('http://').for(:domain) } | ||
| it { is_expected.not_to allow_value('edu').for(:domain) } | ||
| it { is_expected.not_to allow_value('com').for(:domain) } | ||
| it { is_expected.not_to allow_value('co.uk').for(:domain) } | ||
| it { is_expected.not_to allow_value('http://invalid uri').for(:domain) } | ||
| it { is_expected.not_to allow_value('-wrong.edu').for(:domain) } | ||
|
Comment on lines
+79
to
+87
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: Callers (including my work) would like a validator method on this called something like:
valid_domain?(candidate_domain)which returns true ifcandidate_domainis registered for this school.This would allow testing domains presented by registering users without exposing the internal implementation of
school_email_domains.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a validator method to
Schoolusing a straightforwardschool_email_domains.exists?I don't think we need to optimise for batch domain checks, since we're validating one student at a time
If we ever did need to perform some sort of batch validation, we could revisit this and perhaps pluck then memoise the domains