Skip to content
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

Improve display of own door code to members, reject non-numerical or too-short door codes #604

Merged
merged 4 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/door_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class DoorCode < ApplicationRecord
belongs_to :user, optional: true

validates :code, presence: true
validates :code, numericality: { only_integer: true }
validates :code, length: { minimum: 6 }
validates_uniqueness_of :code, case_sensitive: false

class << self
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class User < ApplicationRecord

has_one :profile, dependent: :destroy
has_one :application, dependent: :destroy
has_one :door_code, dependent: :destroy
has_one :door_code
has_many :authentications, dependent: :destroy
has_many :votes, dependent: :destroy
has_many :comments, dependent: :destroy
Expand Down
26 changes: 15 additions & 11 deletions app/views/members/users/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
- if current_user.member? || current_user.key_member?
.mt-20
- if current_user.member?
= link_to "Become a key member", edit_members_user_key_members_path(current_user), class: "btn btn-default"

- unless current_user.voting_policy_agreement
= link_to "Become a voting member", edit_members_user_voting_members_path(current_user), class: "btn btn-default"

= render 'bookmarks'

- if current_user.key_member?
- if current_user.door_code
%p Your door code is: #{current_user.door_code.code}
- else
%p
You don't seem to have a door code set. If you need one, contact
=mail_to MEMBERSHIP_EMAIL
for help.
%h3 Space Access
- if current_user.door_code.present?
%p Your door code is #{current_user.door_code.code}
- elsif current_user.key_member?
%p
You are a key member, but you don't seem to have a door code set. If you need one, contact
=mail_to MEMBERSHIP_EMAIL
for help.
- elsif current_user.member?
%p
You are not a key member.
= link_to "Become a key member", edit_members_user_key_members_path(current_user), class: "btn btn-default"
- else
%p
Only members can become key_members with access to the physical space.

- if @all_admins.any?
%h3 Admins
Expand Down
2 changes: 1 addition & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
end

factory :door_code do
sequence(:code) { |n| "#{1000+n}" }
sequence(:code) { |n| "#{100000+n}" }

trait :assigned do
association :user, factory: :key_member
Expand Down
10 changes: 9 additions & 1 deletion spec/models/door_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
describe "validations" do
it { is_expected.to validate_presence_of(:code) }
it { is_expected.to validate_uniqueness_of(:code).case_insensitive }

it "rejects non-numeric codes" do
expect(DoorCode.new(code: "asdf").valid?).to be false
end

it "rejects too-short codes" do
expect(DoorCode.new(code: "12345").valid?).to be false
end
end

describe ".make_random_code" do
Expand All @@ -20,7 +28,7 @@

describe "#status" do
it "defaults to not_in_lock" do
new_door_code = DoorCode.create!(user: create(:user), code: "12345")
new_door_code = DoorCode.create!(user: create(:user), code: "123456")
expect(new_door_code.not_in_lock?).to be true
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@
end

context "when member had a door code" do
let!(:door_code) { create(:door_code, user: member) }
let!(:door_code) { create(:door_code, user: member, status: :in_lock) }

it "unassigns the door code" do
subject
door_code.reload
expect(door_code.user_id).to eq(nil)
expect(door_code.is_assigned?).to be false
expect(door_code.status).to eq("formerly_assigned_in_lock")
end
end
end
Expand Down