Skip to content

Commit

Permalink
add sunet_id helper method, have factories use email instead of sunet_id
Browse files Browse the repository at this point in the history
  • Loading branch information
SaravShah committed Oct 1, 2018
1 parent fdbad8e commit 9da4457
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class User < ApplicationRecord
devise :remote_user_authenticatable # We don't want other (default) Devise modules
has_many :bundle_contexts
validates :sunet_id, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true

def email
sunet_id.include?('@') ? sunet_id : "#{sunet_id}@stanford.edu"
def sunet_id
email.split('@').first
end
end
2 changes: 1 addition & 1 deletion spec/controllers/bundle_contexts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

context 'POST create' do
context 'Valid Parameters' do
let(:output_dir) { "#{Settings.job_output_parent_dir}/#{subject.current_user.sunet_id}/SMPL-multimedia" }
let(:output_dir) { "#{Settings.job_output_parent_dir}/#{subject.current_user.email}/SMPL-multimedia" }

before { Dir.delete(output_dir) if Dir.exist?(output_dir) }

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :user do
sequence(:sunet_id) { |n| "fake_#{n}@stanford.edu" } # TODO: migrate this to an `email` field
sequence(:email) { |n| "fake_#{n}@stanford.edu" }
end
end
2 changes: 1 addition & 1 deletion spec/models/bundle_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

describe 'output_dir' do
it 'returns "Settings.job_output_parent_dir/user_id/project_name"' do
expect(bc.output_dir).to eq "#{Settings.job_output_parent_dir}/#{bc.user.sunet_id}/#{bc.project_name}"
expect(bc.output_dir).to eq "#{Settings.job_output_parent_dir}/#{bc.user.email}/#{bc.project_name}"
end
end

Expand Down
16 changes: 8 additions & 8 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
RSpec.describe User, type: :model do
subject(:user) { build(:user, sunet_id: 'jdoe@stanford.edu') }
subject(:user) { build(:user, email: 'jdoe@stanford.edu') }

context 'validation' do
it 'is not valid unless it has all required attributes' do
expect(User.new).not_to be_valid
expect(user).to be_valid
end

it { is_expected.to validate_uniqueness_of(:sunet_id) }
it { is_expected.to validate_presence_of(:sunet_id) }
it { is_expected.to validate_uniqueness_of(:email).ignoring_case_sensitivity }
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to have_many(:bundle_contexts) }

describe 'enforces unique constraint on sunet_id' do
describe 'enforces unique constraint on email' do
before { user.save! }

it 'at model level' do
expect { described_class.create!(sunet_id: user.sunet_id) }.to raise_error(ActiveRecord::RecordInvalid)
expect { described_class.create!(email: user.email) }.to raise_error(ActiveRecord::RecordInvalid)
end
it 'at db level' do
expect { user.dup.save!(validate: false) }.to raise_error(ActiveRecord::RecordNotUnique)
Expand All @@ -24,12 +24,12 @@
end

context 'email address' do
it "returns the user's email address if the sunet_id is already an address" do
it "returns the user's email address" do
expect(user.email).to eq('jdoe@stanford.edu')
end

it 'returns a stanford.edu email address if the sunet_id is not an email address' do
expect { user.sunet_id = 'jdoe' }.not_to change(user, :email).from('jdoe@stanford.edu')
it "returns the user's sunet id" do
expect(user.sunet_id).to eq('jdoe')
end
end
end
2 changes: 1 addition & 1 deletion spec/services/discovery_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
bundle_dir: 'spec/test_data/images_jp2_tif',
staging_style_symlink: false,
content_metadata_creation: 0,
user: build(:user, sunet_id: 'jdoe@stanford.edu')
user: build(:user, email: 'jdoe@stanford.edu')
}
end
let(:bundle_context) { BundleContext.new(bc_params) }
Expand Down
2 changes: 1 addition & 1 deletion spec/support/bundle_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def bundle_context_from_hash(proj)
content_structure: hash['project_style']['content_structure'],
bundle_dir: hash['bundle_dir'],
content_metadata_creation: cmc,
user: build(:user, sunet_id: 'Jdoe@stanford.edu')
user: build(:user, email: 'Jdoe@stanford.edu')
)
end

0 comments on commit 9da4457

Please sign in to comment.