okkez-multi_auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. data/README +36 -0
  2. data/app/controllers/application_controller.rb +11 -0
  3. data/app/controllers/auth/email_controller.rb +34 -0
  4. data/app/controllers/auth/open_id_controller.rb +43 -0
  5. data/app/controllers/auth_controller.rb +24 -0
  6. data/app/controllers/credentials/email_controller.rb +156 -0
  7. data/app/controllers/credentials/open_id_controller.rb +85 -0
  8. data/app/controllers/credentials_controller.rb +14 -0
  9. data/app/controllers/signup/email_controller.rb +132 -0
  10. data/app/controllers/signup/open_id_controller.rb +62 -0
  11. data/app/controllers/signup_controller.rb +8 -0
  12. data/app/helpers/application_helper.rb +5 -0
  13. data/app/models/activation_mailer.rb +114 -0
  14. data/app/models/email_credential.rb +89 -0
  15. data/app/models/email_credential_edit_form.rb +52 -0
  16. data/app/models/email_login_form.rb +24 -0
  17. data/app/models/email_password_edit_form.rb +36 -0
  18. data/app/models/open_id_credential.rb +33 -0
  19. data/app/models/open_id_login_form.rb +17 -0
  20. data/app/models/session.rb +19 -0
  21. data/app/models/user.rb +37 -0
  22. data/app/views/activation_mailer/complete_for_credential.erb +4 -0
  23. data/app/views/activation_mailer/complete_for_notice.erb +4 -0
  24. data/app/views/activation_mailer/complete_for_signup.erb +4 -0
  25. data/app/views/activation_mailer/request_for_credential.erb +11 -0
  26. data/app/views/activation_mailer/request_for_notice.erb +11 -0
  27. data/app/views/activation_mailer/request_for_signup.erb +11 -0
  28. data/app/views/auth/email/index.html.erb +90 -0
  29. data/app/views/auth/logged_in.html.erb +21 -0
  30. data/app/views/auth/logged_out.html.erb +21 -0
  31. data/app/views/auth/open_id/index.html.erb +16 -0
  32. data/app/views/credentials/email/activated.html.erb +4 -0
  33. data/app/views/credentials/email/activation.html.erb +15 -0
  34. data/app/views/credentials/email/created.html.erb +15 -0
  35. data/app/views/credentials/email/delete.html.erb +20 -0
  36. data/app/views/credentials/email/edit_password.html.erb +31 -0
  37. data/app/views/credentials/email/new.html.erb +36 -0
  38. data/app/views/credentials/index.html.erb +88 -0
  39. data/app/views/credentials/open_id/delete.html.erb +20 -0
  40. data/app/views/credentials/open_id/new.html.erb +26 -0
  41. data/app/views/signup/email/_progress.html.erb +14 -0
  42. data/app/views/signup/email/activated.html.erb +9 -0
  43. data/app/views/signup/email/activation.html.erb +27 -0
  44. data/app/views/signup/email/created.html.erb +14 -0
  45. data/app/views/signup/email/index.html.erb +34 -0
  46. data/app/views/signup/email/validated.html.erb +21 -0
  47. data/app/views/signup/index.html.erb +51 -0
  48. data/app/views/signup/open_id/authenticated.html.erb +15 -0
  49. data/app/views/signup/open_id/created.html.erb +6 -0
  50. data/app/views/signup/open_id/index.html.erb +16 -0
  51. data/config/boot.rb +110 -0
  52. data/config/database.yml +22 -0
  53. data/config/database.yml.sqlite3 +22 -0
  54. data/config/environment.rb +62 -0
  55. data/config/routes.rb +51 -0
  56. data/config/smtp.yml.example +8 -0
  57. data/db/development.sqlite3 +0 -0
  58. data/db/schema.rb +88 -0
  59. data/db/test.sqlite3 +0 -0
  60. data/lib/action_mailer_util.rb +15 -0
  61. data/lib/multi_auth.rb +64 -0
  62. data/lib/multi_auth_helper.rb +98 -0
  63. data/lib/notice_formatter.rb +106 -0
  64. data/lib/open_id_authentication/result.rb +12 -0
  65. data/lib/token_util.rb +18 -0
  66. data/public/404.html +92 -0
  67. data/public/422.html +91 -0
  68. data/public/500.html +92 -0
  69. data/public/503.html +92 -0
  70. data/public/favicon.ico +0 -0
  71. data/public/images/battery/cell.png +0 -0
  72. data/public/images/battery/level-green.png +0 -0
  73. data/public/images/battery/level-orange.png +0 -0
  74. data/public/images/battery/level-red.png +0 -0
  75. data/public/images/battery/level-yellow.png +0 -0
  76. data/public/images/battery/style.html +82 -0
  77. data/public/images/favicons/livedoor.png +0 -0
  78. data/public/images/favicons/mixi.png +0 -0
  79. data/public/images/favicons/yahoo.png +0 -0
  80. data/public/images/h1-back.png +0 -0
  81. data/public/images/icons/fam/add.png +0 -0
  82. data/public/images/icons/fam/bin.png +0 -0
  83. data/public/images/icons/fam/bomb.png +0 -0
  84. data/public/images/icons/fam/cog.png +0 -0
  85. data/public/images/icons/fam/delete.png +0 -0
  86. data/public/images/icons/fam/email-with-desc.png +0 -0
  87. data/public/images/icons/fam/email.png +0 -0
  88. data/public/images/icons/fam/feed.png +0 -0
  89. data/public/images/icons/fam/help.png +0 -0
  90. data/public/images/icons/fam/key-with-desc.png +0 -0
  91. data/public/images/icons/fam/key.png +0 -0
  92. data/public/images/icons/fam/lightning.png +0 -0
  93. data/public/images/icons/fam/plugin.png +0 -0
  94. data/public/images/icons/fam/stop.png +0 -0
  95. data/public/images/icons/fam/table_save.png +0 -0
  96. data/public/images/icons/fam/tick.png +0 -0
  97. data/public/images/icons/fam/user.png +0 -0
  98. data/public/images/icons/fam/vcard.png +0 -0
  99. data/public/images/icons/openid-with-desc.png +0 -0
  100. data/public/images/icons/openid.png +0 -0
  101. data/public/images/logo-back.png +0 -0
  102. data/public/images/logo.png +0 -0
  103. data/public/images/side-column-back.png +0 -0
  104. data/public/javascripts/application.js +2 -0
  105. data/public/javascripts/controls.js +963 -0
  106. data/public/javascripts/dragdrop.js +973 -0
  107. data/public/javascripts/effects.js +1128 -0
  108. data/public/javascripts/prototype.js +4320 -0
  109. data/public/robots.txt +5 -0
  110. data/public/stylesheets/application.css +365 -0
  111. data/public/stylesheets/auth.css +22 -0
  112. data/public/stylesheets/home.css +114 -0
  113. data/rails/init.rb +24 -0
  114. data/test/functional/auth/email_controller_test.rb +102 -0
  115. data/test/functional/auth/open_id_controller_test.rb +76 -0
  116. data/test/functional/auth_controller_test.rb +74 -0
  117. data/test/functional/credentials/email_controller_test.rb +488 -0
  118. data/test/functional/credentials/open_id_controller_test.rb +308 -0
  119. data/test/functional/credentials_controller_test.rb +49 -0
  120. data/test/functional/signup/email_controller_test.rb +369 -0
  121. data/test/functional/signup/open_id_controller_test.rb +44 -0
  122. data/test/functional/signup_controller_test.rb +17 -0
  123. data/test/performance/browsing_test.rb +9 -0
  124. data/test/test_helper.rb +82 -0
  125. data/test/unit/action_mailer_util_test.rb +63 -0
  126. data/test/unit/activation_mailer_test.rb +181 -0
  127. data/test/unit/email_credential_edit_form_test.rb +173 -0
  128. data/test/unit/email_credential_test.rb +324 -0
  129. data/test/unit/email_login_form_test.rb +76 -0
  130. data/test/unit/email_password_edit_form_test.rb +117 -0
  131. data/test/unit/helpers/auth_helper_test.rb +4 -0
  132. data/test/unit/helpers/credentials/email_helper_test.rb +4 -0
  133. data/test/unit/helpers/credentials/open_id_helper_test.rb +4 -0
  134. data/test/unit/helpers/credentials_helper_test.rb +4 -0
  135. data/test/unit/helpers/email_auth_helper_test.rb +4 -0
  136. data/test/unit/helpers/email_signup_helper_test.rb +4 -0
  137. data/test/unit/helpers/open_id_auth_helper_test.rb +4 -0
  138. data/test/unit/helpers/open_id_signup_helper_test.rb +4 -0
  139. data/test/unit/helpers/password_auth_helper_test.rb +4 -0
  140. data/test/unit/helpers/password_signup_helper_test.rb +4 -0
  141. data/test/unit/helpers/signup_helper_test.rb +4 -0
  142. data/test/unit/notice_formatter_test.rb +153 -0
  143. data/test/unit/open_id_credential_test.rb +108 -0
  144. data/test/unit/open_id_login_form_test.rb +57 -0
  145. data/test/unit/session_test.rb +53 -0
  146. data/test/unit/token_util_test.rb +51 -0
  147. data/test/unit/user_test.rb +177 -0
  148. metadata +220 -0
@@ -0,0 +1,76 @@
1
+
2
+ require 'test_helper'
3
+
4
+ class EmailLoginFormTest < ActiveSupport::TestCase
5
+ def setup
6
+ @klass = EmailLoginForm
7
+ @form = @klass.new
8
+ @basic = @klass.new(
9
+ :email => "email@example.jp",
10
+ :password => "password")
11
+ end
12
+
13
+ #
14
+ # 基底クラス
15
+ #
16
+
17
+ test "superclass" do
18
+ assert_equal(ActiveForm, @klass.superclass)
19
+ end
20
+
21
+ #
22
+ # カラム
23
+ #
24
+
25
+ test "columns" do
26
+ [
27
+ [:email, nil, "str", "str"],
28
+ [:password, nil, "str", "str"],
29
+ ].each { |name, default, set_value, get_value|
30
+ form = @klass.new
31
+ assert_equal(default, form.__send__(name))
32
+ form.__send__("#{name}=", set_value)
33
+ assert_equal(get_value, form.__send__(name))
34
+ }
35
+ end
36
+
37
+ #
38
+ # 検証
39
+ #
40
+
41
+ test "basic is valid" do
42
+ assert_equal(true, @basic.valid?)
43
+ end
44
+
45
+ test "validates_presence_of :email" do
46
+ @basic.email = nil
47
+ assert_equal(false, @basic.valid?)
48
+ end
49
+
50
+ test "validates_presence_of :password" do
51
+ @basic.password = nil
52
+ assert_equal(false, @basic.valid?)
53
+ end
54
+
55
+ #
56
+ # インスタンスメソッド
57
+ #
58
+
59
+ test "authenticate, success" do
60
+ @form.email = email_credentials(:yuya_gmail).email
61
+ @form.password = "yuya_gmail"
62
+ assert_equal(
63
+ email_credentials(:yuya_gmail),
64
+ @form.authenticate)
65
+ end
66
+
67
+ test "authenticate, not activated" do
68
+ @form.email = email_credentials(:yuya_nayutaya).email
69
+ @form.password = "yuya_nayutaya"
70
+ assert_equal(nil, @form.authenticate)
71
+ end
72
+
73
+ test "authenticate, empty" do
74
+ assert_equal(nil, @form.authenticate)
75
+ end
76
+ end
@@ -0,0 +1,117 @@
1
+
2
+ require 'test_helper'
3
+
4
+ class EmailPasswordEditFormTest < ActiveSupport::TestCase
5
+ def setup
6
+ @klass = EmailPasswordEditForm
7
+ @form = @klass.new
8
+ @basic = @klass.new(
9
+ :password => "password",
10
+ :password_confirmation => "password")
11
+ end
12
+
13
+ #
14
+ # 基底クラス
15
+ #
16
+
17
+ test "superclass" do
18
+ assert_equal(ActiveForm, @klass.superclass)
19
+ end
20
+
21
+ #
22
+ # カラム
23
+ #
24
+
25
+ test "columns" do
26
+ [
27
+ [:password, nil, "1", "1"],
28
+ [:password_confirmation, nil, "1", "1"],
29
+ ].each { |name, default, set_value, get_value|
30
+ form = @klass.new
31
+ assert_equal(default, form.__send__(name), name)
32
+ form.__send__("#{name}=", set_value)
33
+ assert_equal(get_value, form.__send__(name), name)
34
+ }
35
+ end
36
+
37
+ #
38
+ # 検証
39
+ #
40
+
41
+ test "basic is valid" do
42
+ assert_equal(true, @basic.valid?)
43
+ end
44
+
45
+ test "validates_presence_of :password" do
46
+ @basic.password = nil
47
+ assert_equal(false, @basic.valid?)
48
+ end
49
+
50
+ test "validates_presence_of :password_confirmation" do
51
+ @basic.password_confirmation = nil
52
+ assert_equal(false, @basic.valid?)
53
+ end
54
+
55
+ test "validates_length_of :password" do
56
+ [
57
+ ["a" * 3, false],
58
+ ["a" * 4, true ],
59
+ ["a" * 20, true ],
60
+ ["a" * 21, false],
61
+ ].each { |value, expected|
62
+ @basic.password = value
63
+ @basic.password_confirmation = value
64
+ assert_equal(expected, @basic.valid?, value)
65
+ }
66
+ end
67
+
68
+ test "validates_format_of :password" do
69
+ valid_chars = (0x21..0x7E).map { |c| c.chr }.join
70
+
71
+ [
72
+ [valid_chars.slice!(0, 20), true ],
73
+ [valid_chars.slice!(0, 20), true ],
74
+ [valid_chars.slice!(0, 20), true ],
75
+ [valid_chars.slice!(0, 20), true ],
76
+ [valid_chars.slice!(0, 20), true ],
77
+ ["aaaa", true ],
78
+ ["aaa ", false],
79
+ ["日本語", false],
80
+ ].each { |value, expected|
81
+ @basic.password = value
82
+ @basic.password_confirmation = value
83
+ assert_equal(expected, @basic.valid?, value)
84
+ }
85
+
86
+ assert_equal(true, valid_chars.empty?)
87
+ end
88
+
89
+ test "validates_each, password" do
90
+ @basic.password = "aaaa"
91
+ @basic.password_confirmation = "aaaa"
92
+ assert_equal(true, @basic.valid?)
93
+
94
+ @basic.password = "aaaa"
95
+ @basic.password_confirmation = "AAAA"
96
+ assert_equal(false, @basic.valid?)
97
+ end
98
+
99
+ #
100
+ # インスタンスメソッド
101
+ #
102
+
103
+ test "to_email_credential_hash, empty" do
104
+ hash = @form.to_email_credential_hash
105
+ assert_equal([:hashed_password], hash.keys)
106
+ assert_equal(true, EmailCredential.compare_hashed_password("", hash[:hashed_password]))
107
+ end
108
+
109
+ test "to_email_credential_hash, full" do
110
+ @form.attributes = {
111
+ :password => "foo",
112
+ }
113
+ hash = @form.to_email_credential_hash
114
+ assert_equal([:hashed_password], hash.keys)
115
+ assert_equal(true, EmailCredential.compare_hashed_password(@form.password, hash[:hashed_password]))
116
+ end
117
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class AuthHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class Credentials::EmailHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class Credentials::OpenIdHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class CredentialsHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class EmailAuthHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class EmailSignupHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class OpenIdAuthHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class OpenIdSignupHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordAuthHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordSignupHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class SignupHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,153 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+
5
+ class NoticeFormatterTest < ActiveSupport::TestCase
6
+ def setup
7
+ @module = NoticeFormatter
8
+ end
9
+
10
+ test "replace_keywords, empty" do
11
+ assert_equal("", @module.replace_keywords("", {}))
12
+ end
13
+
14
+ test "replace_keywords" do
15
+ assert_equal(
16
+ "A",
17
+ @module.replace_keywords("{a}", "a" => "A"))
18
+ assert_equal(
19
+ "A,B",
20
+ @module.replace_keywords("{a},{b}", "a" => "A", "b" => "B"))
21
+ assert_equal(
22
+ "A,B,A,B",
23
+ @module.replace_keywords("{a},{b},{a},{b}", "a" => "A", "b" => "B"))
24
+ end
25
+
26
+ test "add_namespace" do
27
+ expected = {
28
+ "name:a" => "b",
29
+ "name:c" => "d",
30
+ }
31
+ assert_equal(expected, @module.add_namespace("name", {"a" => "b", "c" => "d"}))
32
+ end
33
+
34
+ test "format_integer_value" do
35
+ assert_equal("1", @module.format_integer_value(1))
36
+ assert_equal("-", @module.format_integer_value(nil))
37
+ end
38
+
39
+ test "format_integer_json_value" do
40
+ assert_equal("1", @module.format_integer_json_value(1))
41
+ assert_equal("null", @module.format_integer_json_value(nil))
42
+ end
43
+
44
+ test "format_string_value" do
45
+ assert_equal("a", @module.format_string_value("a"))
46
+ assert_equal("あ", @module.format_string_value("あ"))
47
+ assert_equal("-", @module.format_string_value(""))
48
+ assert_equal("-", @module.format_string_value(nil))
49
+ end
50
+
51
+ test "format_string_json_value" do
52
+ assert_equal('"a"', @module.format_string_json_value("a"))
53
+ assert_equal('"\\u3042"', @module.format_string_json_value("あ"))
54
+ assert_equal("null", @module.format_string_json_value(""))
55
+ assert_equal("null", @module.format_string_json_value(nil))
56
+ end
57
+
58
+ test "format_part_of_date" do
59
+ expected = {
60
+ "date" => "2009-01-02",
61
+ "date:json" => '"2009-01-02"',
62
+ "date:ja" => "2009年01月02日",
63
+ "yyyy" => "2009",
64
+ "mm" => "01",
65
+ "dd" => "02",
66
+ }
67
+ assert_equal(expected, @module.format_part_of_date(Date.new(2009, 1, 2)))
68
+ end
69
+
70
+ test "format_part_of_date, nil" do
71
+ expected = {
72
+ "date" => "-",
73
+ "date:json" => "null",
74
+ "date:ja" => "-",
75
+ "yyyy" => "-",
76
+ "mm" => "-",
77
+ "dd" => "-",
78
+ }
79
+ assert_equal(expected, @module.format_part_of_date(nil))
80
+ end
81
+
82
+ test "format_part_of_time" do
83
+ expected = {
84
+ "time" => "01:02:03",
85
+ "time:json" => '"01:02:03"',
86
+ "time:ja" => "01時02分03秒",
87
+ "hh" => "01",
88
+ "nn" => "02",
89
+ "ss" => "03",
90
+ }
91
+ assert_equal(expected, @module.format_part_of_time(Time.local(2009, 12, 31, 1, 2, 3)))
92
+ end
93
+
94
+ test "format_part_of_time, nil" do
95
+ expected = {
96
+ "time" => "-",
97
+ "time:json" => "null",
98
+ "time:ja" => "-",
99
+ "hh" => "-",
100
+ "nn" => "-",
101
+ "ss" => "-",
102
+ }
103
+ assert_equal(expected, @module.format_part_of_time(nil))
104
+ end
105
+
106
+ test "format_part_of_datetime" do
107
+ datetime = Time.local(2009, 12, 31, 12, 34, 56)
108
+ expected = {
109
+ "datetime" => "2009-12-31 12:34:56",
110
+ "datetime:json" => '"2009-12-31 12:34:56"',
111
+ "datetime:ja" => "2009年12月31日 12時34分56秒",
112
+ }
113
+ expected.merge!(@module.format_part_of_date(datetime))
114
+ expected.merge!(@module.format_part_of_time(datetime))
115
+ assert_equal(expected, @module.format_part_of_datetime(datetime))
116
+ end
117
+
118
+ test "format_part_of_datetime, nil" do
119
+ expected = {
120
+ "datetime" => "-",
121
+ "datetime:json" => "null",
122
+ "datetime:ja" => "-",
123
+ }
124
+ expected.merge!(@module.format_part_of_date(nil))
125
+ expected.merge!(@module.format_part_of_time(nil))
126
+ assert_equal(expected, @module.format_part_of_datetime(nil))
127
+ end
128
+
129
+ test "format_part_of_user" do
130
+ expected = {
131
+ "user:token" => "0" * User::TokenLength,
132
+ "user:token:json" => '"' + "0" * User::TokenLength + '"',
133
+ "user:nickname" => "nickname",
134
+ "user:nickname:json" => '"nickname"',
135
+ }
136
+ user = User.new(
137
+ :user_token => "0" * User::TokenLength,
138
+ :nickname => "nickname")
139
+ assert_equal(expected, @module.format_part_of_user(user))
140
+ end
141
+
142
+ test "format_part_of_user, nil" do
143
+ expected = {
144
+ "user:token" => "-",
145
+ "user:token:json" => "null",
146
+ "user:nickname" => "-",
147
+ "user:nickname:json" => "null",
148
+ }
149
+ assert_equal(expected, @module.format_part_of_user(User.new))
150
+ assert_equal(expected, @module.format_part_of_user(nil))
151
+ end
152
+
153
+ end
@@ -0,0 +1,108 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+
5
+ class OpenIdCredentialTest < ActiveSupport::TestCase
6
+ def setup
7
+ @klass = OpenIdCredential
8
+ @basic = @klass.new(
9
+ :user_id => users(:yuya).id,
10
+ :identity_url => "http://example.jp/identity_url")
11
+
12
+ @yuya_livedoor = open_id_credentials(:yuya_livedoor)
13
+ @shinya_example = open_id_credentials(:shinya_example)
14
+ end
15
+
16
+ #
17
+ # 関連
18
+ #
19
+
20
+ test "belongs_to :user" do
21
+ assert_equal(
22
+ users(:yuya),
23
+ @yuya_livedoor.user)
24
+
25
+ assert_equal(
26
+ users(:shinya),
27
+ @shinya_example.user)
28
+ end
29
+
30
+ #
31
+ # 検証
32
+ #
33
+
34
+ test "all fixtures are valid" do
35
+ assert_equal(true, @klass.all.all?(&:valid?))
36
+ end
37
+
38
+ test "basic is valid" do
39
+ assert_equal(true, @basic.valid?)
40
+ end
41
+
42
+ test "validates_presence_of :identity_url" do
43
+ @basic.identity_url = nil
44
+ assert_equal(false, @basic.valid?)
45
+ assert_equal(true, @basic.errors.invalid?(:identity_url))
46
+ end
47
+
48
+ test "validates_length_of :identity_url" do
49
+ @basic.identity_url = "http://example.com/" + "a" * 182
50
+ assert_equal(201, @basic.identity_url.size)
51
+
52
+ assert_equal(false, @basic.valid?)
53
+ assert_equal(true, @basic.errors.invalid?(:identity_url))
54
+ end
55
+
56
+ test "validates_format_of :identity_url" do
57
+ [
58
+ ["http://example.com/foo", true, false],
59
+ ["https://example.com/foo", true, false],
60
+ ["ftp://example.com/foo", false, true ],
61
+ ["HTTP://EXAMPLE.COM/foo", false, true ],
62
+ ].each{|value, expected1, expected2|
63
+ @basic.identity_url = value
64
+ assert_equal(expected1, @basic.valid?)
65
+ assert_equal(expected2, @basic.errors.invalid?(:identity_url))
66
+ }
67
+ end
68
+
69
+ test "validates_uniqueness_of :identity_url, on create" do
70
+ @basic.identity_url = @yuya_livedoor.identity_url
71
+ assert_equal(false, @basic.valid?)
72
+ end
73
+
74
+ test "validates_uniqueness_of :identity_url, on update" do
75
+ @yuya_livedoor.identity_url = @shinya_example.identity_url
76
+ assert_equal(false, @yuya_livedoor.valid?)
77
+ end
78
+
79
+ test "validates_each :user_id" do
80
+ srand(0)
81
+ user = users(:yuya)
82
+ create_record = proc {
83
+ user.open_id_credentials.create!(
84
+ :identity_url => "http://example.jp/identity_url#{rand(1000)}")
85
+ }
86
+
87
+ assert_nothing_raised {
88
+ (10 - user.open_id_credentials.size).times {
89
+ record = create_record[]
90
+ record.save!
91
+ }
92
+ }
93
+ assert_raise(ActiveRecord::RecordInvalid) {
94
+ create_record[]
95
+ }
96
+ end
97
+
98
+ #
99
+ # インスタンスメソッド
100
+ #
101
+
102
+ test "login!" do
103
+ time = Time.local(2010, 1, 1)
104
+ assert_equal(nil, @shinya_example.loggedin_at)
105
+ Kagemusha::DateTime.at(time) { @shinya_example.login! }
106
+ assert_equal(time, @shinya_example.reload.loggedin_at)
107
+ end
108
+ end
@@ -0,0 +1,57 @@
1
+
2
+ require 'test_helper'
3
+
4
+ class OpenIdLoginFormTest < ActiveSupport::TestCase
5
+ def setup
6
+ @klass = OpenIdLoginForm
7
+ @basic = @klass.new(
8
+ :openid_url => "example.jp")
9
+ end
10
+
11
+ #
12
+ # 基底クラス
13
+ #
14
+
15
+ test "superclass" do
16
+ assert_equal(ActiveForm, @klass.superclass)
17
+ end
18
+
19
+ #
20
+ # カラム
21
+ #
22
+
23
+ test "columns" do
24
+ [
25
+ [:openid_url, nil, "1", "1"],
26
+ ].each { |name, default, set_value, get_value|
27
+ form = @klass.new
28
+ assert_equal(default, form.__send__(name), name)
29
+ form.__send__("#{name}=", set_value)
30
+ assert_equal(get_value, form.__send__(name), name)
31
+ }
32
+ end
33
+
34
+ #
35
+ # 検証
36
+ #
37
+
38
+ test "basic is valid" do
39
+ assert_equal(true, @basic.valid?)
40
+ end
41
+
42
+ test "validates_presence_of :openid_url" do
43
+ @basic.openid_url = nil
44
+ assert_equal(false, @basic.valid?)
45
+ end
46
+
47
+ test "validates_length_of :openid_url" do
48
+ [
49
+ ["a" * 1, true ],
50
+ ["a" * 200, true ],
51
+ ["a" * 201, false],
52
+ ].each { |value, expected|
53
+ @basic.openid_url = value
54
+ assert_equal(expected, @basic.valid?, value)
55
+ }
56
+ end
57
+ end
@@ -0,0 +1,53 @@
1
+
2
+ require 'test_helper'
3
+
4
+ class SessionTest < ActiveSupport::TestCase
5
+ def setup
6
+ @klass = Session
7
+ @record = @klass.new
8
+ end
9
+
10
+ #
11
+ # クラスメソッド
12
+ #
13
+
14
+ test "self.cleanup, 1 hour" do
15
+ session_a = create_empty_session(Time.local(2009, 1, 1, 11, 59, 59), "a")
16
+ session_b = create_empty_session(Time.local(2009, 1, 1, 12, 0, 0), "b")
17
+ session_c = create_empty_session(Time.local(2009, 1, 1, 12, 0, 1), "c")
18
+
19
+ assert_difference("Session.count", -1) {
20
+ Kagemusha::DateTime.at(2009, 1, 1, 13, 0, 0) {
21
+ assert_equal(nil, @klass.cleanup(1.hour))
22
+ }
23
+ }
24
+
25
+ assert_nil(@klass.find_by_id(session_a.id))
26
+ assert_not_nil(@klass.find_by_id(session_b.id))
27
+ assert_not_nil(@klass.find_by_id(session_c.id))
28
+ end
29
+
30
+ test "self.cleanup, 2 hours" do
31
+ session_a = create_empty_session(Time.local(2009, 12, 31, 0, 30, 29), "a")
32
+ session_b = create_empty_session(Time.local(2009, 12, 31, 0, 30, 30), "b")
33
+ session_c = create_empty_session(Time.local(2009, 12, 31, 0, 30, 31), "c")
34
+
35
+ assert_difference("Session.count", -1) {
36
+ Kagemusha::DateTime.at(2009, 12, 31, 2, 30, 30) {
37
+ assert_equal(nil, @klass.cleanup(2.hours))
38
+ }
39
+ }
40
+
41
+ assert_nil(@klass.find_by_id(session_a.id))
42
+ assert_not_nil(@klass.find_by_id(session_b.id))
43
+ assert_not_nil(@klass.find_by_id(session_c.id))
44
+ end
45
+
46
+ private
47
+
48
+ def create_empty_session(time, key)
49
+ return Kagemusha::DateTime.at(time) {
50
+ Session.create!(:session_id => key, :data => "")
51
+ }
52
+ end
53
+ end
@@ -0,0 +1,51 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+
5
+ class TokenUtilTest < ActiveSupport::TestCase
6
+ def setup
7
+ @mod = TokenUtil
8
+ end
9
+
10
+ test "create_token" do
11
+ assert_match(/\A[0-9a-f]{10}\z/, @mod.create_token(10))
12
+ assert_match(/\A[0-9a-f]{20}\z/, @mod.create_token(20))
13
+ assert_match(/\A[0-9a-f]{30}\z/, @mod.create_token(30))
14
+
15
+ srand(0)
16
+ assert_not_equal(
17
+ @mod.create_token(20),
18
+ @mod.create_token(20))
19
+ end
20
+
21
+ test "create_unique_token" do
22
+ assert_match(/\A[0-9a-f]{10}\z/, @mod.create_unique_token(User, :user_token, 10))
23
+ assert_match(/\A[0-9a-f]{20}\z/, @mod.create_unique_token(User, :user_token, 20))
24
+ assert_match(/\A[0-9a-f]{30}\z/, @mod.create_unique_token(User, :user_token, 30))
25
+
26
+ srand(0)
27
+ assert_not_equal(
28
+ @mod.create_unique_token(User, :user_token, 20),
29
+ @mod.create_unique_token(User, :user_token, 20))
30
+ end
31
+
32
+ test "create_unique_token, conflict" do
33
+ [
34
+ [User, :user_token],
35
+ ].each { |klass, column|
36
+ exist_token = klass.first[column]
37
+ unique_token = "0" * exist_token.size
38
+ assert_equal(true, klass.exists?(column => exist_token))
39
+ assert_equal(false, klass.exists?(column => unique_token))
40
+
41
+ tokens = [exist_token, unique_token]
42
+ musha = Kagemusha.new(@mod)
43
+ musha.defs(:create_token) { tokens.shift }
44
+ musha.swap {
45
+ assert_equal(
46
+ unique_token,
47
+ @mod.create_unique_token(klass, column, exist_token.size))
48
+ }
49
+ }
50
+ end
51
+ end