Skip to content

Commit

Permalink
Add back the functionality of saving a new Author instance if the Sal…
Browse files Browse the repository at this point in the history
…mon message is verified
  • Loading branch information
carols10cents committed Apr 15, 2012
1 parent 5d374a0 commit 5b5431d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
21 changes: 17 additions & 4 deletions app/models/salmon_interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def interpret
# We can ignore salmon for authors that have a local user account.
return true if local_user?

@author = find_or_initialize_author

raise RstatUs::InvalidSalmonMessage unless message_verified?

if @author.new?
@author.save!
end
end

# Isolating calls to external classes so we can stub these methods in test
Expand All @@ -29,13 +35,20 @@ def self.parse(body)
OStatus::Salmon.from_xml body
end

def self.find_or_create_author
end

private

def find_or_initialize_author
author = Author.first :remote_url => author_uri

# This author is unknown to us, so let's create a new Author
unless author
end

author
end

def message_verified?
@salmon.verified?(public_key)
@salmon.verified?(@author.retrieve_public_key)
end

def local_user?
Expand Down
27 changes: 27 additions & 0 deletions test/models/salmon_interpreter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,39 @@ class InvalidSalmonMessage < StandardError; end
end

it "raises an exception if we can't verify the salmon message" do
@s.stubs(:find_or_initialize_author)
@s.expects(:message_verified?).returns(false)

lambda {
@s.interpret
}.must_raise RstatUs::InvalidSalmonMessage
end

describe "unseen" do
it "saves the new Author if the message is verified" do
author = mock
author.stubs(:new?).returns(true)
@s.expects(:find_or_initialize_author).returns(author)
@s.expects(:message_verified?).returns(true)

author.expects(:save!)

@s.interpret
end

it "doesn't save the new Author if the message fails verification" do
author = mock
author.stubs(:new?).returns(true)
@s.expects(:find_or_initialize_author).returns(author)
@s.expects(:message_verified?).returns(false)

author.expects(:save!).never

lambda {
@s.interpret
}.must_raise RstatUs::InvalidSalmonMessage
end
end
end
end
end

0 comments on commit 5b5431d

Please sign in to comment.