You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
8.1 KiB

  1. require 'rails_helper'
  2. RSpec.describe TagManager do
  3. describe '#local_domain?' do
  4. # The following comparisons MUST be case-insensitive.
  5. around do |example|
  6. original_local_domain = Rails.configuration.x.local_domain
  7. Rails.configuration.x.local_domain = 'domain'
  8. example.run
  9. Rails.configuration.x.local_domain = original_local_domain
  10. end
  11. it 'returns true for nil' do
  12. expect(TagManager.instance.local_domain?(nil)).to eq true
  13. end
  14. it 'returns true if the slash-stripped string equals to local domain' do
  15. expect(TagManager.instance.local_domain?('DoMaIn/')).to eq true
  16. end
  17. it 'returns false for irrelevant string' do
  18. expect(TagManager.instance.local_domain?('DoMaIn!')).to eq false
  19. end
  20. end
  21. describe '#web_domain?' do
  22. # The following comparisons MUST be case-insensitive.
  23. around do |example|
  24. original_web_domain = Rails.configuration.x.web_domain
  25. Rails.configuration.x.web_domain = 'domain'
  26. example.run
  27. Rails.configuration.x.web_domain = original_web_domain
  28. end
  29. it 'returns true for nil' do
  30. expect(TagManager.instance.web_domain?(nil)).to eq true
  31. end
  32. it 'returns true if the slash-stripped string equals to web domain' do
  33. expect(TagManager.instance.web_domain?('DoMaIn/')).to eq true
  34. end
  35. it 'returns false for string with irrelevant characters' do
  36. expect(TagManager.instance.web_domain?('DoMaIn!')).to eq false
  37. end
  38. end
  39. describe '#normalize_domain' do
  40. it 'returns nil if the given parameter is nil' do
  41. expect(TagManager.instance.normalize_domain(nil)).to eq nil
  42. end
  43. it 'returns normalized domain' do
  44. expect(TagManager.instance.normalize_domain('DoMaIn/')).to eq 'domain'
  45. end
  46. end
  47. describe '#local_url?' do
  48. around do |example|
  49. original_local_domain = Rails.configuration.x.local_domain
  50. example.run
  51. Rails.configuration.x.local_domain = original_local_domain
  52. end
  53. it 'returns true if the normalized string with port is local URL' do
  54. Rails.configuration.x.local_domain = 'domain:42'
  55. expect(TagManager.instance.local_url?('https://DoMaIn:42/')).to eq true
  56. end
  57. it 'returns true if the normalized string without port is local URL' do
  58. Rails.configuration.x.local_domain = 'domain'
  59. expect(TagManager.instance.local_url?('https://DoMaIn/')).to eq true
  60. end
  61. it 'returns false for string with irrelevant characters' do
  62. Rails.configuration.x.local_domain = 'domain'
  63. expect(TagManager.instance.local_url?('https://domainn/')).to eq false
  64. end
  65. end
  66. describe '#same_acct?' do
  67. # The following comparisons MUST be case-insensitive.
  68. it 'returns true if the needle has a correct username and domain for remote user' do
  69. expect(TagManager.instance.same_acct?('username@domain', 'UsErNaMe@DoMaIn')).to eq true
  70. end
  71. it 'returns false if the needle is missing a domain for remote user' do
  72. expect(TagManager.instance.same_acct?('username@domain', 'UsErNaMe')).to eq false
  73. end
  74. it 'returns false if the needle has an incorrect domain for remote user' do
  75. expect(TagManager.instance.same_acct?('username@domain', 'UsErNaMe@incorrect')).to eq false
  76. end
  77. it 'returns false if the needle has an incorrect username for remote user' do
  78. expect(TagManager.instance.same_acct?('username@domain', 'incorrect@DoMaIn')).to eq false
  79. end
  80. it 'returns true if the needle has a correct username and domain for local user' do
  81. expect(TagManager.instance.same_acct?('username', 'UsErNaMe@Cb6E6126.nGrOk.Io')).to eq true
  82. end
  83. it 'returns true if the needle is missing a domain for local user' do
  84. expect(TagManager.instance.same_acct?('username', 'UsErNaMe')).to eq true
  85. end
  86. it 'returns false if the needle has an incorrect username for local user' do
  87. expect(TagManager.instance.same_acct?('username', 'UsErNaM@Cb6E6126.nGrOk.Io')).to eq false
  88. end
  89. it 'returns false if the needle has an incorrect domain for local user' do
  90. expect(TagManager.instance.same_acct?('username', 'incorrect@Cb6E6126.nGrOk.Io')).to eq false
  91. end
  92. end
  93. describe '#unique_tag' do
  94. it 'returns a unique tag' do
  95. expect(TagManager.instance.unique_tag(Time.utc(2000), 12, 'Status')).to eq 'tag:cb6e6126.ngrok.io,2000-01-01:objectId=12:objectType=Status'
  96. end
  97. end
  98. describe '#unique_tag_to_local_id' do
  99. it 'returns the ID part' do
  100. expect(TagManager.instance.unique_tag_to_local_id('tag:cb6e6126.ngrok.io,2000-01-01:objectId=12:objectType=Status', 'Status')).to eql '12'
  101. end
  102. it 'returns nil if it is not local id' do
  103. expect(TagManager.instance.unique_tag_to_local_id('tag:remote,2000-01-01:objectId=12:objectType=Status', 'Status')).to eq nil
  104. end
  105. it 'returns nil if it is not expected type' do
  106. expect(TagManager.instance.unique_tag_to_local_id('tag:cb6e6126.ngrok.io,2000-01-01:objectId=12:objectType=Block', 'Status')).to eq nil
  107. end
  108. it 'returns nil if it does not have object ID' do
  109. expect(TagManager.instance.unique_tag_to_local_id('tag:cb6e6126.ngrok.io,2000-01-01:objectType=Status', 'Status')).to eq nil
  110. end
  111. end
  112. describe '#local_id?' do
  113. it 'returns true for a local ID' do
  114. expect(TagManager.instance.local_id?('tag:cb6e6126.ngrok.io;objectId=12:objectType=Status')).to be true
  115. end
  116. it 'returns false for a foreign ID' do
  117. expect(TagManager.instance.local_id?('tag:foreign.tld;objectId=12:objectType=Status')).to be false
  118. end
  119. end
  120. describe '#uri_for' do
  121. subject { TagManager.instance.uri_for(target) }
  122. context 'activity object' do
  123. let(:target) { Fabricate(:status, reblog: Fabricate(:status)).stream_entry }
  124. before { target.update!(created_at: '2000-01-01T00:00:00Z') }
  125. it 'returns the unique tag for status' do
  126. expect(target.object_type).to eq :activity
  127. is_expected.to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{target.id}:objectType=Status"
  128. end
  129. end
  130. context 'comment object' do
  131. let(:target) { Fabricate(:status, created_at: '2000-01-01T00:00:00Z', reply: true) }
  132. it 'returns the unique tag for status' do
  133. expect(target.object_type).to eq :comment
  134. is_expected.to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{target.id}:objectType=Status"
  135. end
  136. end
  137. context 'note object' do
  138. let(:target) { Fabricate(:status, created_at: '2000-01-01T00:00:00Z', reply: false, thread: nil) }
  139. it 'returns the unique tag for status' do
  140. expect(target.object_type).to eq :note
  141. is_expected.to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{target.id}:objectType=Status"
  142. end
  143. end
  144. context 'person object' do
  145. let(:target) { Fabricate(:account, username: 'alice') }
  146. it 'returns the URL for account' do
  147. expect(target.object_type).to eq :person
  148. is_expected.to eq 'https://cb6e6126.ngrok.io/users/alice'
  149. end
  150. end
  151. end
  152. describe '#url_for' do
  153. let(:alice) { Fabricate(:account, username: 'alice') }
  154. subject { TagManager.instance.url_for(target) }
  155. context 'activity object' do
  156. let(:target) { Fabricate(:status, account: alice, reblog: Fabricate(:status)).stream_entry }
  157. it 'returns the unique tag for status' do
  158. expect(target.object_type).to eq :activity
  159. is_expected.to eq "https://cb6e6126.ngrok.io/@alice/#{target.id}"
  160. end
  161. end
  162. context 'comment object' do
  163. let(:target) { Fabricate(:status, account: alice, reply: true) }
  164. it 'returns the unique tag for status' do
  165. expect(target.object_type).to eq :comment
  166. is_expected.to eq "https://cb6e6126.ngrok.io/@alice/#{target.id}"
  167. end
  168. end
  169. context 'note object' do
  170. let(:target) { Fabricate(:status, account: alice, reply: false, thread: nil) }
  171. it 'returns the unique tag for status' do
  172. expect(target.object_type).to eq :note
  173. is_expected.to eq "https://cb6e6126.ngrok.io/@alice/#{target.id}"
  174. end
  175. end
  176. context 'person object' do
  177. let(:target) { alice }
  178. it 'returns the URL for account' do
  179. expect(target.object_type).to eq :person
  180. is_expected.to eq 'https://cb6e6126.ngrok.io/@alice'
  181. end
  182. end
  183. end
  184. end