Permalink
Browse files

test the approval logic

  • Loading branch information...
technoweenie committed May 9, 2011
1 parent 312ac00 commit 90460f76e82cc63ad381b182720adba8bc88e812
Showing with 28 additions and 4 deletions.
  1. +12 −3 lib/chat_gram/model.rb
  2. +16 −1 test/app_test.rb
View
@@ -47,19 +47,28 @@ def initialize(options = {})
#
# Returns true if the user is approved, or false.
def approved?(username)
- !@db[:users].where(:username => username).count.zero?
+ user = @db[:users].where(:username => username).first
+ user && !user[:token].to_s.empty?
end
# Public: Approves the given user and assigns their OAuth token.
#
# username - The String Instagram username.
# token - The String OAuth token.
#
- # Returns nothing.
+ # Returns true if the user is approved, or false.
def approve(username, token)
- @db[:users].
+ return false if token.to_s.empty?
+
+ num = @db[:users].
where(:username => username).
update(:token => token)
+
+ if num > 1
+ raise "Multiple users named #{username.inspect}"
+ else
+ num == 1
+ end
end
# Inserts the given user data into the db.
View
@@ -61,7 +61,22 @@ def speak(text)
:model => (@@model = ChatGram::Model::Database.new(:url => 'sqlite:/'))
@@model.setup
- @@model.insert 'user!'
+ @@model.insert 'user!', 'abc'
+
+ def test_approves_existing_user
+ @@model.insert('existing')
+ assert !@@model.approved?('existing')
+ assert_equal false, @@model.approve('existing', '')
+ assert !@@model.approved?('existing')
+ assert_equal true, @@model.approve('existing', 'foobar')
+ assert @@model.approved?('existing')
+ end
+
+ def test_does_not_approve_non_existent_user
+ assert !@@model.approved?('newb')
+ @@model.approve('newb', 'abc')
+ assert !@@model.approved?('newb')
+ end
def test_receives_webhook
@instagram.stubs.get("/v1/users/1234/media/recent.json?") { stubbed_image }

0 comments on commit 90460f7

Please sign in to comment.