openskip-skip_embedded 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/ChangeLog +4 -0
  2. data/README.rdoc +37 -0
  3. data/Rakefile +87 -0
  4. data/generators/skip_embedded/skip_embedded_generator.rb +30 -0
  5. data/generators/skip_embedded/templates/app/views/shared/_flash_message.html.erb +13 -0
  6. data/generators/skip_embedded/templates/app/views/shared/_skip_full_text_search_form.html.erb +24 -0
  7. data/generators/skip_embedded/templates/public/javascripts/dropdown_navigation.js +12 -0
  8. data/generators/skip_embedded/templates/public/javascripts/labeled_text_field.js +21 -0
  9. data/generators/skip_embedded/templates/public/javascripts/skip_fckeditor_config.js +45 -0
  10. data/generators/skip_embedded/templates/public/stylesheets/skip_embedded.css +79 -0
  11. data/lib/skip_embedded/fulltext_search_cache/builder_base.rb +51 -0
  12. data/lib/skip_embedded/fulltext_search_cache/mediator.rb +67 -0
  13. data/lib/skip_embedded/fulltext_search_cache/partial_loader.rb +19 -0
  14. data/lib/skip_embedded/fulltext_search_cache.rb +38 -0
  15. data/lib/skip_embedded/helpers/ui.rb +46 -0
  16. data/lib/skip_embedded/helpers.rb +13 -0
  17. data/lib/skip_embedded/initial_settings.rb +28 -0
  18. data/lib/skip_embedded/op_fixation.rb +43 -0
  19. data/lib/skip_embedded/open_id_sso/authentication.rb +21 -0
  20. data/lib/skip_embedded/open_id_sso/session_management.rb +42 -0
  21. data/lib/skip_embedded/web_service_util/client.rb +75 -0
  22. data/lib/skip_embedded/web_service_util/server.rb +14 -0
  23. data/lib/skip_embedded/web_service_util.rb +26 -0
  24. data/lib/skip_embedded.rb +4 -0
  25. data/rails/init.rb +6 -0
  26. data/spec/skip_embedded/fulltext_search_cache/builder_base_spec.rb +44 -0
  27. data/spec/skip_embedded/fulltext_search_cache/mediator_spec.rb +49 -0
  28. data/spec/skip_embedded/fulltext_search_cache/partial_loader_spec.rb +20 -0
  29. data/spec/skip_embedded/initial_setting_spec.rb +10 -0
  30. data/spec/skip_embedded/op_fixation_spec.rb +57 -0
  31. data/spec/skip_embedded/web_service_util_spec.rb +197 -0
  32. data/spec/spec_helper.rb +16 -0
  33. metadata +114 -0
@@ -0,0 +1,21 @@
1
+ module SkipEmbedded
2
+ module OpenIdSso
3
+ module Authentication
4
+ def self.included(base)
5
+ base.alias_method_chain :access_denied, :open_id_sso
6
+ end
7
+
8
+ private
9
+ def access_denied_with_open_id_sso(message = nil)
10
+ if op = SkipEmbedded::OpFixation.sso_openid_provider_url
11
+ store_location
12
+ authenticate_with_open_id(op, :method => "post", :return_to=>session_url, :required => SessionsController.attribute_adapter.keys) do
13
+ access_denied_without_open_id_sso(message)
14
+ end
15
+ else
16
+ access_denied_without_open_id_sso(message)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,42 @@
1
+
2
+ module SkipEmbedded
3
+ module OpenIdSso
4
+ module SessionManagement
5
+ def self.included(controller)
6
+ controller.before_filter :available_op_limitation, :only => %w[create]
7
+ end
8
+
9
+ private
10
+ def after_logout_path
11
+ OpFixation.sso_openid_logout_url || login_path
12
+ end
13
+
14
+ def login_successfully(user, personal_data)
15
+ if OpFixation.sso_enabled?
16
+ data = attribute_adapter.adapt(personal_data)
17
+ user.update_attributes!(data.slice(:display_name))
18
+ flash[:notice] = _("Successfully synchronized your display name with OP's")
19
+ end
20
+ super
21
+ end
22
+
23
+ def signup(identity_url, personal_data = {})
24
+ session[:user] = attribute_adapter.adapt(personal_data) if OpFixation.sso_enabled?
25
+ super
26
+ end
27
+
28
+ def available_op_limitation
29
+ if params[:openid_url] && !OpFixation.accept?(params[:openid_url])
30
+ logger.debug("login refused since #{params[:openid_url]} is not member #{OpFixation.servers.inspect}")
31
+ authenticate_failure
32
+ elsif OpFixation.sso_enabled? && !params["open_id_complete"].blank? && (params["openid.identity"] != params["openid.claimed_id"])
33
+ logger.debug("login refused since claimed_id differs from identity.")
34
+ authenticate_failure
35
+ else
36
+ true
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require "net/https"
4
+ require 'uri'
5
+ require 'erb'
6
+
7
+ require 'skip_embedded/initial_settings'
8
+
9
+ module SkipEmbedded
10
+ class WebServiceUtil
11
+ module Client
12
+ attr_writer :logger
13
+ def logger
14
+ @logger ||= if defined?(ActiveRecord::Base)
15
+ ActiveRecord::Base.logger
16
+ elsif defined?(Rails)
17
+ Rails.logger
18
+ elsif
19
+ require 'logger'
20
+ Logger.new
21
+ end
22
+ end
23
+ # 別のWebアプリのWeb-APIをコールするためのユーティリティメソッド
24
+ # Webアプリ連携の際は、このメソッドを経由して利用すること
25
+ # 引数
26
+ # :app_name = 呼び出したいWebアプリケーションのシンボル
27
+ # :service_name = 呼び出したいWeb-APIの名前
28
+ # :params = 呼び出す際のパラメータ
29
+ # :controller_name = サービスのコントローラパス(デフォルトの規約は"services")
30
+ # services以外を指定も可能
31
+ def open_service app_name, service_name, params={}, controller_name="services"
32
+ ActiveSupport::Deprecation.warn("use 'open_service_with_url' with specified endopoint.")
33
+
34
+ app_url, app_ca_file = if app = InitialSettings['collaboration_apps'][app_name.to_s]
35
+ [ app["url"], app["ca_file"] ]
36
+ else
37
+ [nil, nil]
38
+ end
39
+ url = "#{app_url}/#{controller_name}/#{service_name}"
40
+ self.open_service_with_url(url, params, app_ca_file)
41
+ end
42
+
43
+ def open_service_with_url url, params = {}, app_ca_file = nil
44
+ url = "#{url}?" +
45
+ params.map{|key, val| "#{ERB::Util.u(key.to_s)}=#{ERB::Util.u(val.to_s)}"}.join('&')
46
+ self.get_json(url, app_ca_file)
47
+ end
48
+
49
+ def get_json(url, ca_file = nil)
50
+ uri = URI.parse url
51
+ http = Net::HTTP.new(uri.host, uri.port)
52
+ if uri.port == 443
53
+ http.use_ssl = true
54
+ if ca_file
55
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
56
+ http.ca_file = ca_file
57
+ http.verify_depth = 5
58
+ else
59
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
60
+ end
61
+ end
62
+ response = http.get("#{uri.path}?#{uri.query}", "X-SECRET-KEY" => InitialSettings['skip_collaboration']['secret_key'])
63
+ if response.code == "200"
64
+ JSON.parse(response.body)
65
+ else
66
+ logger.error "[WebServiceUtil Error] Response code is #{response.code} to access #{url}"
67
+ nil
68
+ end
69
+ rescue Errno::ECONNREFUSED, OpenSSL::SSL::SSLError, SocketError, ArgumentError, JSON::ParserError, URI::InvalidURIError => ex
70
+ logger.error "[WebServiceUtil Error] #{ex.to_s} to access #{url}"
71
+ nil
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,14 @@
1
+ module SkipEmbedded
2
+ # FIXME moduleにする
3
+ class WebServiceUtil
4
+ module Server
5
+ def check_secret_key
6
+ req_key = request.env["HTTP_X_SECRET_KEY"] && request.env["HTTP_X_SECRET_KEY"]
7
+ unless req_key == InitialSettings['skip_collaboration']['secret_key']
8
+ render :text => { :error => "Forbidden" }.to_json, :status => :forbidden
9
+ return false
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ # SKIP(Social Knowledge & Innovation Platform)
2
+ # Copyright (C) 2008 TIS Inc.
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # アプリケーション連携用のライブラリ
17
+ # 別のWebアプリを呼び出す際は、WebServiceUtilを利用する
18
+ # 呼び出されるサービスを定義する際は、ForServicesModuleをincludeする
19
+ require 'skip_embedded/web_service_util/client'
20
+
21
+ module SkipEmbedded
22
+ class WebServiceUtil
23
+ extend SkipEmbedded::WebServiceUtil::Client
24
+ end
25
+ end
26
+
@@ -0,0 +1,4 @@
1
+ # SkipEmbedded
2
+ module SkipEmbedded
3
+ Version = "0.0.15"
4
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,6 @@
1
+ # Include hook code here
2
+ require 'skip_embedded/initial_settings'
3
+ Thread.critical{
4
+ SkipEmbedded::InitialSettings.config = File.expand_path("config/initial_settings.yml", Rails.root)
5
+ SkipEmbedded.instance # force loading
6
+ }
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:set fileencoding=utf-8 filetype=ruby
3
+
4
+ require File.expand_path("../../spec_helper", File.dirname(__FILE__))
5
+ require 'skip_embedded/fulltext_search_cache/builder_base'
6
+
7
+ describe SkipEmbedded::FulltextSearchCache::BuilderBase, :type => :model do
8
+ before do
9
+ @url_writer = mock("url_writer")
10
+
11
+ SkipEmbedded::FulltextSearchCache::BuilderBase.stub!(:url_writer).and_return(@url_writer)
12
+ SkipEmbedded::FulltextSearchCache::BuilderBase.entity_name = "target"
13
+
14
+ @it = SkipEmbedded::FulltextSearchCache::BuilderBase.new("--- entity ---")
15
+ @it.stub!(:root_url).and_return("http://asset.example.com/")
16
+ end
17
+
18
+ it{ @it.target.should == "--- entity ---" }
19
+
20
+ it{ @it.icon_url.should == "http://asset.example.com/images/icons/target.gif" }
21
+
22
+ it "to_metaはovverrideしてつかうこと" do
23
+ lambda{ @it.to_meta }.should raise_error(NotImplementedError)
24
+ end
25
+ it "titleはovverrideしてつかうこと" do
26
+ lambda{ @it.title }.should raise_error(NotImplementedError)
27
+ end
28
+ it "bodyはovverrideしてつかうこと" do
29
+ lambda{ @it.body }.should raise_error(NotImplementedError)
30
+ end
31
+
32
+ it "write_metaは、mediatorが返すパスにto_meta.to_yamlを書き込むこと" do
33
+ File.should_receive(:open).with("path", "wb").and_yield(fd = mock("fd"))
34
+ fd.should_receive(:write).with("--- meta ---".to_yaml)
35
+
36
+ mediator = mock("mediator")
37
+ mediator.should_receive(:meta_path).and_return("path")
38
+
39
+ @it.should_receive(:to_meta).and_return("--- meta ---")
40
+
41
+ @it.write_meta(mediator)
42
+ end
43
+ end
44
+
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'skip_embedded/fulltext_search_cache/mediator'
3
+
4
+ describe SkipEmbedded::FulltextSearchCache, "new('path/to/cache/root', :since => 15.seconds.ago)", :type => :model do
5
+ before do
6
+ @fts_cache = SkipEmbedded::FulltextSearchCache::Mediator.new(:cache_dir => 'path/to/cache/root/app_cache', :url_prefix => "http://example.com", :since => 15.seconds.ago)
7
+ end
8
+
9
+ it{ @fts_cache.cache_dir.should == 'path/to/cache/root/app_cache' }
10
+
11
+ describe "出力先パスの算出" do
12
+ before do
13
+ @builder = mock("builder")
14
+ @builder.should_receive(:filename).and_return "file.html"
15
+ end
16
+
17
+ it do
18
+ @fts_cache.cache_path(@builder).should == "#{Dir.pwd}/path/to/cache/root/app_cache/file.html"
19
+ end
20
+
21
+ it do
22
+ @fts_cache.meta_path(@builder).should == "#{Dir.pwd}/path/to/cache/root/app_cache_meta/file.html"
23
+ end
24
+ end
25
+
26
+ describe "#build" do
27
+
28
+ before do
29
+ @model_klass = mock("model_klass")
30
+ @model_klass.should_receive(:name).and_return "MdelClass"
31
+ @model_klass.should_receive(:quoted_table_name).and_return "mdoel_classes"
32
+ @model_klass.should_receive(:scoped).and_return( [model_obj = mock("model_obj")] )
33
+ model_obj.should_receive(:id).and_return("--id--")
34
+
35
+ SkipEmbedded::FulltextSearchCache::PartialLoader.should_receive(:new).with([model_obj], 1000).and_return(loader = mock("loader"))
36
+ loader.should_receive(:each).and_yield( model_obj )
37
+
38
+ @builder_klass = mock("builder_klass")
39
+ @builder_klass.should_receive(:new).with( model_obj ).and_return( builder = mock("builder") )
40
+ builder.should_receive(:write_cache).with(@fts_cache)
41
+ builder.should_receive(:write_meta).with(@fts_cache)
42
+ end
43
+
44
+ it do
45
+ @fts_cache.build(@model_klass, @builder_klass)
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'skip_embedded/fulltext_search_cache/partial_loader'
3
+
4
+ describe SkipEmbedded::FulltextSearchCache::PartialLoader, "new(mock_note, 100)", :type => :model do
5
+ before do
6
+ @klass = mock("mock_note")
7
+ @klass.should_receive(:count).and_return 101
8
+ @klass.should_receive(:find).with(:all, :limit => 100, :offset => 0).and_return((1..99).to_a)
9
+ @klass.should_receive(:find).with(:all, :limit => 100, :offset => 100).and_return [100, 101]
10
+
11
+ @loader = SkipEmbedded::FulltextSearchCache::PartialLoader.new(@klass, 100)
12
+ end
13
+
14
+ it do
15
+ mock_block = mock("block")
16
+ mock_block.should_receive(:call).exactly(101).times
17
+ @loader.each{|n| mock_block.call }
18
+ end
19
+ end
20
+
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'skip_embedded/initial_settings'
3
+
4
+ describe SkipEmbedded::InitialSettings do
5
+ # see spec/initial_setting.yml
6
+ it{
7
+ SkipEmbedded::InitialSettings[:skip_collaboration]["skip_url"].should == "http://test.openskip.org"
8
+ }
9
+ end
10
+
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'skip_embedded/op_fixation'
3
+
4
+ describe SkipEmbedded::OpFixation, "http://openid.example.comで擬似SSOをする場合", :type => :model do
5
+ before() do
6
+ SkipEmbedded::OpFixation.sso_openid_provider_url = "http://openid.example.com/"
7
+ end
8
+
9
+ it "sso_openid_provider_urlは@server_endpoint_urlであること" do
10
+ SkipEmbedded::OpFixation.sso_openid_provider_url.should == "http://openid.example.com/"
11
+ end
12
+
13
+ it "sso_openid_logout_urlは@server_endpoint_url + '/logout'であること" do
14
+ SkipEmbedded::OpFixation.sso_openid_logout_url.should == "http://openid.example.com/logout"
15
+ end
16
+ end
17
+
18
+ describe SkipEmbedded::OpFixation, "[http://openid.example.com]を受け付ける場合", :type => :model do
19
+ before(:all) do
20
+ @server_endpoint_url = "http://openid.example.com/server"
21
+ SkipEmbedded::OpFixation.servers = @server_endpoint_url
22
+ end
23
+
24
+ describe "OpenID.discoverのOP endpoint URLがhttp://openid.example.comの場合" do
25
+ before do
26
+ mock_op_service = mock("op_service")
27
+ mock_op_service.should_receive(:server_url).and_return @server_endpoint_url
28
+ OpenID.should_receive(:discover).and_return [:ignore, [mock_op_service]]
29
+ end
30
+
31
+ it "http://openid.example.com/alice/での問い合わせは許可すること" do
32
+ SkipEmbedded::OpFixation.accept?("http://openid.example.com/alice/").should be_true
33
+ end
34
+ end
35
+
36
+ describe "OpenID.discoverのOP endpoint URLがhttp://not-registered.example.comの場合" do
37
+ before do
38
+ mock_op_service = mock("op_service")
39
+ mock_op_service.should_receive(:server_url).and_return "http://not-registered.example.com"
40
+ OpenID.should_receive(:discover).and_return [:ignore, [mock_op_service]]
41
+ end
42
+
43
+ it "http://not-registered.example.com/alice/での問い合わせは許可しないこと" do
44
+ SkipEmbedded::OpFixation.accept?("http://not-registered.example.com/alice/").should be_false
45
+ end
46
+ end
47
+ end
48
+
49
+ describe SkipEmbedded::OpFixation, "serversを指定しない場合", :type => :model do
50
+ before(:all) do
51
+ SkipEmbedded::OpFixation.servers = []
52
+ end
53
+
54
+ it "http://not-registered.example.com/alice/での問い合わせを許可すること" do
55
+ SkipEmbedded::OpFixation.accept?("http://not-registered.example.com/alice/").should be_true
56
+ end
57
+ end
@@ -0,0 +1,197 @@
1
+ # SKIP(Social Knowledge & Innovation Platform)
2
+ # Copyright (C) 2008 TIS Inc.
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require File.dirname(__FILE__) + '/../spec_helper'
17
+ require 'skip_embedded/web_service_util'
18
+
19
+ module SkipEmbedded
20
+ describe WebServiceUtil do
21
+ before do
22
+ @logger = mock("logger")
23
+ WebServiceUtil.logger = @logger
24
+ end
25
+
26
+ describe ".open_service" do
27
+ before do
28
+ ActiveSupport::Deprecation.should_receive(:warn)
29
+ SkipEmbedded::InitialSettings.stub!(:[]).with("collaboration_apps").and_return("app" => {"url" => "http://testapp.host"})
30
+ @base = "http://testapp.host/services/user_info"
31
+ end
32
+ it "get_jsonをパラメータからURLを作りだすこと" do
33
+ WebServiceUtil.should_receive(:open_service_with_url).with(@base, { :id => "user", :key => "key" }, nil)
34
+ WebServiceUtil.open_service "app", "user_info", { :id => "user", :key => "key" }
35
+ end
36
+ end
37
+
38
+ describe "open_service_with_url" do
39
+ it "paramsがエンコードされてget_jsonに渡されること" do
40
+ params = { :id => "user", :key => "ほげ%$# ふが" }
41
+ query_str = "key=%E3%81%BB%E3%81%92%25%24%23%20%E3%81%B5%E3%81%8C&id=user"
42
+
43
+ WebServiceUtil.should_receive(:get_json).with("url?#{query_str}", nil)
44
+ WebServiceUtil.open_service_with_url "url", params
45
+ end
46
+ end
47
+
48
+ describe ".get_json" do
49
+ before do
50
+ @result = { "symbol" => "user", "type" => "entry"}
51
+ @response = mock('response', :code => "200", :body => @result.to_json)
52
+ @http = mock('http', :get => @response)
53
+ end
54
+ describe "レスポンスが正しく返ってくる場合" do
55
+ before do
56
+ Net::HTTP.stub!(:new).and_return(@http)
57
+ end
58
+ describe "httpの場合" do
59
+ it "bodyがパースされて返されること" do
60
+ WebServiceUtil.get_json("http://test.host/services/user_info?user_code=user").should == @result
61
+ end
62
+ end
63
+ describe "httpsの場合" do
64
+ before do
65
+ @http.stub!(:use_ssl=)
66
+ @http.stub!(:verify_mode=)
67
+ end
68
+ it "bodyがパースされて返されること" do
69
+ WebServiceUtil.get_json("https://test.host/services/user_info?user_code=user").should == @result
70
+ end
71
+ it "httpがuse_ssl = trueで呼ばれること" do
72
+ @http.should_receive(:use_ssl=).with(true)
73
+ WebServiceUtil.get_json("https://test.host/services/user_info?user_code=user")
74
+ end
75
+ it "httpがverify_mode = OpenSSL::SSL::VERIFY_NONE で呼ばれること" do
76
+ @http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
77
+ WebServiceUtil.get_json("https://test.host/services/user_info?user_code=user")
78
+ end
79
+ describe "httpsでca_fileありの場合" do
80
+ before do
81
+ @ca_file = "path/to/config/server.pem"
82
+ @http.stub!(:ca_file=)
83
+ @http.stub!(:verify_depth=)
84
+ end
85
+ it "bodyがパースされて返されること" do
86
+ WebServiceUtil.get_json("https://test.host/services/user_info?user_code=user", @ca_file).should == @result
87
+ end
88
+ it "httpに ca_file が設定されること" do
89
+ @http.should_receive(:ca_file=).with(@ca_file)
90
+ WebServiceUtil.get_json("https://test.host/services/user_info?user_code=user", @ca_file)
91
+ end
92
+ it "httpに verify_depth が設定されること" do
93
+ @http.should_receive(:verify_depth=).with(5)
94
+ WebServiceUtil.get_json("https://test.host/services/user_info?user_code=user", @ca_file)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ describe "引数がnilの場合" do
100
+ it "nilが返ること" do
101
+ @logger.stub!(:error)
102
+ WebServiceUtil.get_json(nil).should be_nil
103
+ end
104
+ it "ログが出力されること" do
105
+ @logger.should_receive(:error).with(/[WebServiceUtil Error] .*/)
106
+ WebServiceUtil.get_json(nil).should be_nil
107
+ end
108
+ end
109
+ describe "返ってくるものが正しくparseできない場合" do
110
+ before do
111
+ Net::HTTP.stub!(:new).and_return(@http)
112
+ @response.stub!(:body).and_return("ぱーすにしっぱいする")
113
+ @url = "http://test.host/services/user_info?user_code=user"
114
+ end
115
+ it "nilが返ること" do
116
+ @logger.stub!(:error)
117
+ WebServiceUtil.get_json(@url).should be_nil
118
+ end
119
+ it "ログが出力されること" do
120
+ @logger.should_receive(:error).with(/[WebServiceUtil Error] .*/)
121
+ WebServiceUtil.get_json(@url).should be_nil
122
+ end
123
+ end
124
+ describe "ホストの無いURLが渡された場合" do
125
+ before do
126
+ @url = "/services/user_info?user_code=user"
127
+ end
128
+ it "nilが返ること" do
129
+ @logger.stub!(:error)
130
+ WebServiceUtil.get_json(@url).should be_nil
131
+ end
132
+ it "ログが出力されること" do
133
+ @logger.should_receive(:error).with(/[WebServiceUtil Error] .*/)
134
+ WebServiceUtil.get_json(@url).should be_nil
135
+ end
136
+ end
137
+ describe "レスポンスが404の場合" do
138
+ before do
139
+ @url = "/services/user_info?user_code=user"
140
+ end
141
+ before do
142
+ http = mock('http', :get => mock('response', :code => "404"))
143
+ Net::HTTP.stub!(:new).and_return(http)
144
+ end
145
+ it "nilが返ること" do
146
+ @logger.stub!(:error)
147
+ WebServiceUtil.get_json(@url).should be_nil
148
+ end
149
+ it "ログが出力されること" do
150
+ @logger.should_receive(:error).with("[WebServiceUtil Error] Response code is 404 to access #{@url}")
151
+ WebServiceUtil.get_json(@url).should be_nil
152
+ end
153
+ end
154
+
155
+ describe "ホストが見つからない場合" do
156
+ before do
157
+ @url = "http://test1.host/services/user_info?user_code=user"
158
+ end
159
+ it "nilが返ること" do
160
+ @logger.stub!(:error)
161
+ WebServiceUtil.get_json(@url).should be_nil
162
+ end
163
+ it "ログが出力されること" do
164
+ @logger.should_receive(:error).with(/[WebServiceUtil Error]/)
165
+ WebServiceUtil.get_json(@url).should be_nil
166
+ end
167
+ end
168
+
169
+ # 証明書でSSL認証を行なう場合、以下のエラーが起こる。環境が無いのでコメントアウト
170
+ # 自己証明書の場合 => hostname was not match with the server certificate
171
+ # 証明書が正しくない => SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
172
+ describe "証明書が正しくない場合場合" do
173
+ before do
174
+ mock_http = Net::HTTP.new("www.openskip.org", 443)
175
+
176
+ Net::HTTP.should_receive(:new).with("www.openskip.org", 443).and_return(mock_http)
177
+ mock_http.should_receive(:get).and_raise OpenSSL::SSL::SSLError
178
+
179
+
180
+ @url = "https://www.openskip.org/services/user_info?user_code=user"
181
+ @ca_file = "path/to/config/server.pem"
182
+ end
183
+
184
+ it "nilが返ること" do
185
+ SkipEmbedded::WebServiceUtil.logger = Logger.new("/dev/null")
186
+ SkipEmbedded::WebServiceUtil.get_json(@url, @ca_file).should be_nil
187
+ end
188
+
189
+ it "ログが出力されること" do
190
+ @logger.should_receive(:error).with("[WebServiceUtil Error] OpenSSL::SSL::SSLError to access #{@url}")
191
+
192
+ SkipEmbedded::WebServiceUtil.get_json(@url, @ca_file).should be_nil
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:set fileencoding=utf-8 filetype=ruby
3
+
4
+ require 'rubygems'
5
+
6
+ gem 'rails', '2.1.2'
7
+ require 'active_support'
8
+ require 'action_controller'
9
+
10
+ $LOAD_PATH.unshift File.expand_path("../../../../", File.dirname(__FILE__))
11
+ $LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
12
+
13
+ RAILS_ENV="test"
14
+ require 'skip_embedded/initial_settings'
15
+ SkipEmbedded::InitialSettings.config = File.expand_path("initial_settings.yml", File.dirname(__FILE__))
16
+