Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

TAs can now login from multiple locations. Closes #82

  • Loading branch information...
commit a6d62040696a14d72dce062963995e9979a621ce 1 parent 5361c27
@pwightman pwightman authored
View
19 app/controllers/tas_controller.rb
@@ -12,9 +12,15 @@ def show
end
def create
- @ta = @queue.tas.new(params[:ta])
+ @ta = @queue.tas.where(username: params[:ta][:username]).first
+ if @ta
+ @ta.login_count += 1
+ else
+ @ta = @queue.tas.new(params[:ta])
+ end
+ @ta.update_attributes(params[:ta])
respond_with do |f|
- if @ta.save
+ if @ta.check_password! && @ta.save
sign_in_user @ta
@current_user = @ta
push_notify!
@@ -38,9 +44,16 @@ def update
end
def destroy
- @ta.destroy
+ if @ta.login_count > 1
+ @ta.login_count -= 1
+ @ta.save
+ else
+ @ta.destroy
+ end
+
sign_out_user @ta
push_notify!
+
respond_with do |f|
f.html { redirect_to build_queue_login_path @ta.queue }
end
View
17 app/models/ta.rb
@@ -4,6 +4,7 @@ class Ta < QueueUser
# ATTRIBUTES
attr_accessor :password
+ field :login_count, type: Integer, default: 1
# ASSOCIATIONS
@@ -13,7 +14,7 @@ class Ta < QueueUser
# VALIDATIONS
- validate :check_password, :on => :create
+ validate :check_password!, :on => :create
validates :username, uniqueness: { scope: :school_queue_id }
# SCOPES
@@ -42,11 +43,13 @@ def accept_next_student
end
end
- private
-
- def check_password
- if self.password != self.queue.password
- self.errors["password"] = "is invalid"
- end
+ def check_password!
+ if self.password != self.queue.password
+ self.errors["password"] = "is invalid"
+ return false
+ else
+ return true
end
+ end
+
end
View
50 spec/controllers/tas_controller_spec.rb
@@ -139,21 +139,43 @@
@full_ta_hash.merge!({ :id => res_hash['id'], :token => res_hash['token']})
end
+ it "increments login_count on successful login" do
+ @queue.tas.destroy_all
+ ta = @queue.tas.create!(@full_ta_hash.merge(password: @queue.password))
+ ta.login_count.should == 1
+
+ post :create, { :ta => @full_ta_hash.merge({ :password => @queue.password }) }.merge(@queue_hash)
+
+ response.code.should == "201"
+
+ ta = Ta.find(ta.id)
+ ta.login_count.should == 2
+ end
it "fails to create a ta without proper password" do
- post :create, { :ta => @full_ta_hash, :password => "wrong_password" }.merge(@queue_hash)
+ @queue.tas.destroy_all
+ post :create, { :ta => @full_ta_hash.merge({ :password => "wrong_password" }) }.merge(@queue_hash)
response.code.should == "422"
end
- it "fails to create a ta with same username in same queue" do
+ it "fails to create TA without proper password even if they are already logged in elsewhere" do
ta_hash = attributes_for(:ta)
+ @queue.tas.destroy_all
@queue.tas.create!(ta_hash)
- post :create, { :ta => ta_hash, :password => @queue.password }.merge(@queue_hash)
+ post :create, { :ta => ta_hash.merge({ :password => "wrong_password" }) }.merge(@queue_hash)
response.code.should == "422"
end
+ it "logs in a TA with same username in same queue" do
+ ta_hash = attributes_for(:ta)
+ @queue.tas.create!(ta_hash)
+ post :create, { :ta => ta_hash.merge({ :password => @queue.password }) }.merge(@queue_hash)
+
+ response.code.should == "201"
+ end
+
it "successfully creates a ta with same username in different queue" do
queue2 = @instructor.queues.create!(attributes_for(:school_queue))
@@ -161,7 +183,7 @@
queue2.tas.create!(@full_ta_hash.merge({ password: queue2.password }) )
- post :create, { :ta => ta_hash, :password => @queue.password }.merge(@queue_hash)
+ post :create, { :ta => ta_hash.merge({ :password => @queue.password }) }.merge(@queue_hash)
response.code.should == "201"
end
@@ -187,7 +209,9 @@
response.code.should == "204"
end
- it "successfully destroys the student" do
+ it "successfully destroys the TA if only one is logged in" do
+ @queue.tas.destroy_all
+ @queue.tas.create!(@full_ta_hash.merge(password: @queue.password))
authenticate QueueUser.where(:_id => @full_ta_hash[:id]).first
delete :destroy, { :id => @full_ta_hash[:id] }
@@ -195,6 +219,22 @@
QueueUser.where(:_id => @full_ta_hash[:id]).first.should be_nil
end
+
+ it "only decrements the login_count of the TA if more than one logged in" do
+ @queue.tas.destroy_all
+ ta = @queue.tas.create!(@full_ta_hash.merge(password: @queue.password))
+ ta.login_count = 2
+ ta.save!
+ authenticate ta
+
+ delete :destroy, { :id => @full_ta_hash[:id] }
+
+ response.code.should == "204"
+
+ QueueUser.where(:_id => @full_ta_hash[:id]).first.should_not be_nil
+ ta = Ta.find(ta.id)
+ ta.login_count.should == 1
+ end
end
describe "Authentication" do
Please sign in to comment.
Something went wrong with that request. Please try again.