Commit 8e4458d15ba4eb9adbd5f6ae382b04c4c7d53651

Authored by Alessandro Beltrão
1 parent ec98aeed

Writing tests for webfinger entry point

lib/noosfero/api/federation/webfinger.rb
... ... @@ -27,8 +27,8 @@ def valid_domain?
27 27 domain = params[:resource].split("@")[1]
28 28 environment.domains.map(&:name).include? domain
29 29 else
30   - #please validate domain with http
31   - false
  30 + domain = params[:resource].split("/")[2]
  31 + environment.domains.map(&:name).include? domain
32 32 end
33 33 end
34 34  
... ... @@ -44,16 +44,16 @@ def acct_hash
44 44 end
45 45  
46 46 def extract_person_identifier
47   - person = params[:resource].split("@")[0].split(":")[1]
  47 + params[:resource].split("@")[0].split(":")[1]
48 48 end
49 49  
50 50 def valid_uri?(url)
51 51 uri = URI.parse(url)
52 52 uri.kind_of?(URI::HTTP)
53   - rescue URI::BadURIError
54   - "URI is valid, bad usage is not"
55   - rescue URI::InvalidURIError
56   - "invalid URI"
  53 + rescue URI::BadURIError => ex
  54 + Rails.logger.error "Bad URI Error: #{ex}"
  55 + rescue URI::InvalidURIError => ex
  56 + Rails.logger.error "Invalid URI Error: #{ex}"
57 57 end
58 58  
59 59 def uri_hash
... ... @@ -62,7 +62,6 @@ def uri_hash
62 62 entity = entity_exists?(params[:resource])
63 63 id = params[:resource].split('/').last.to_i
64 64 uri[:properties] = entity.classify.constantize.find(id)
65   - puts uri.inspect
66 65 uri
67 66 end
68 67  
... ... @@ -70,7 +69,6 @@ def entity_exists?(uri)
70 69 possible_entity = uri.split('/')
71 70 possible_entity.map! {|entity| "#{entity}s"}
72 71 ( ActiveRecord::Base.connection.tables & possible_entity ).first
73   - rescue ActiveRecord::RecordNotFound
74   - false
  72 + rescue ActiveRecord::RecordNotFound => ex
  73 + Rails.logger.error "Entity not found on records: #{ex}"
75 74 end
76   -
... ...
test/api/federation/webfinger_test.rb
... ... @@ -3,10 +3,20 @@ require_relative '../test_helper'
3 3 class WebfingerTest < ActiveSupport::TestCase
4 4  
5 5 def setup
  6 + login_api
6 7 end
7 8  
8   - should 'list tasks of environment' do
9   - assert_equal true, true
  9 + should 'return correct user via webfinger url' do
  10 + get '.well-known/webfinger?resource=acct%3Aze%40example.com'
  11 + webfinger = JSON.parse(last_response.body)
  12 + assert_equal 200, last_response.status
  13 + assert_equal webfinger['subject'], 'acct:ze@example.com'
10 14 end
11 15  
  16 + should 'return correct article via webfinger url' do
  17 + get '.well-known/webfinger?resource=http://example.com/article/id/1'
  18 + webfinger = JSON.parse(last_response.body)
  19 + assert_equal 200, last_response.status
  20 + assert_equal webfinger['subject'], 'http://example.com/article/id/1'
  21 + end
12 22 end
... ...