logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

user_spec.rb (10387B)


  1. require 'rails_helper'
  2. require 'devise_two_factor/spec_helpers'
  3. RSpec.describe User, type: :model do
  4. it_behaves_like 'two_factor_backupable'
  5. describe 'otp_secret' do
  6. it 'is encrypted with OTP_SECRET environment variable' do
  7. user = Fabricate(:user,
  8. encrypted_otp_secret: "Fttsy7QAa0edaDfdfSz094rRLAxc8cJweDQ4BsWH/zozcdVA8o9GLqcKhn2b\nGi/V\n",
  9. encrypted_otp_secret_iv: 'rys3THICkr60BoWC',
  10. encrypted_otp_secret_salt: '_LMkAGvdg7a+sDIKjI3mR2Q==')
  11. expect(user.otp_secret).to eq 'anotpsecretthatshouldbeencrypted'
  12. end
  13. end
  14. describe 'validations' do
  15. it 'is invalid without an account' do
  16. user = Fabricate.build(:user, account: nil)
  17. user.valid?
  18. expect(user).to model_have_error_on_field(:account)
  19. end
  20. it 'is invalid without a valid locale' do
  21. user = Fabricate.build(:user, locale: 'toto')
  22. user.valid?
  23. expect(user).to model_have_error_on_field(:locale)
  24. end
  25. it 'is invalid without a valid email' do
  26. user = Fabricate.build(:user, email: 'john@')
  27. user.valid?
  28. expect(user).to model_have_error_on_field(:email)
  29. end
  30. it 'is valid with an invalid e-mail that has already been saved' do
  31. user = Fabricate.build(:user, email: 'invalid-email')
  32. user.save(validate: false)
  33. expect(user.valid?).to be true
  34. end
  35. it 'cleans out empty string from languages' do
  36. user = Fabricate.build(:user, filtered_languages: [''])
  37. user.valid?
  38. expect(user.filtered_languages).to eq []
  39. end
  40. end
  41. describe 'scopes' do
  42. describe 'recent' do
  43. it 'returns an array of recent users ordered by id' do
  44. user_1 = Fabricate(:user)
  45. user_2 = Fabricate(:user)
  46. expect(User.recent).to eq [user_2, user_1]
  47. end
  48. end
  49. describe 'admins' do
  50. it 'returns an array of users who are admin' do
  51. user_1 = Fabricate(:user, admin: false)
  52. user_2 = Fabricate(:user, admin: true)
  53. expect(User.admins).to match_array([user_2])
  54. end
  55. end
  56. describe 'confirmed' do
  57. it 'returns an array of users who are confirmed' do
  58. user_1 = Fabricate(:user, confirmed_at: nil)
  59. user_2 = Fabricate(:user, confirmed_at: Time.now)
  60. expect(User.confirmed).to match_array([user_2])
  61. end
  62. end
  63. describe 'inactive' do
  64. it 'returns a relation of inactive users' do
  65. specified = Fabricate(:user, current_sign_in_at: 15.days.ago)
  66. Fabricate(:user, current_sign_in_at: 13.days.ago)
  67. expect(User.inactive).to match_array([specified])
  68. end
  69. end
  70. describe 'matches_email' do
  71. it 'returns a relation of users whose email starts with the given string' do
  72. specified = Fabricate(:user, email: 'specified@spec')
  73. Fabricate(:user, email: 'unspecified@spec')
  74. expect(User.matches_email('specified')).to match_array([specified])
  75. end
  76. end
  77. describe 'with_recent_ip_address' do
  78. it 'returns a relation of users who is, or was at last time, online with the given IP address' do
  79. specifieds = [
  80. Fabricate(:user, current_sign_in_ip: '0.0.0.42', last_sign_in_ip: '0.0.0.0'),
  81. Fabricate(:user, current_sign_in_ip: nil, last_sign_in_ip: '0.0.0.42')
  82. ]
  83. Fabricate(:user, current_sign_in_ip: '0.0.0.0', last_sign_in_ip: '0.0.0.0')
  84. expect(User.with_recent_ip_address('0.0.0.42')).to match_array(specifieds)
  85. end
  86. end
  87. end
  88. let(:account) { Fabricate(:account, username: 'alice') }
  89. let(:password) { 'abcd1234' }
  90. describe 'blacklist' do
  91. around(:each) do |example|
  92. old_blacklist = Rails.configuration.x.email_blacklist
  93. Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
  94. example.run
  95. Rails.configuration.x.email_domains_blacklist = old_blacklist
  96. end
  97. it 'should allow a non-blacklisted user to be created' do
  98. user = User.new(email: 'foo@example.com', account: account, password: password)
  99. expect(user.valid?).to be_truthy
  100. end
  101. it 'should not allow a blacklisted user to be created' do
  102. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  103. expect(user.valid?).to be_falsey
  104. end
  105. it 'should not allow a subdomain blacklisted user to be created' do
  106. user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password)
  107. expect(user.valid?).to be_falsey
  108. end
  109. end
  110. describe '#confirmed?' do
  111. it 'returns true when a confirmed_at is set' do
  112. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  113. expect(user.confirmed?).to be true
  114. end
  115. it 'returns false if a confirmed_at is nil' do
  116. user = Fabricate.build(:user, confirmed_at: nil)
  117. expect(user.confirmed?).to be false
  118. end
  119. end
  120. describe '#confirm' do
  121. it 'sets email to unconfirmed_email' do
  122. user = Fabricate.build(:user, confirmed_at: Time.now.utc, unconfirmed_email: 'new-email@example.com')
  123. user.confirm
  124. expect(user.email).to eq 'new-email@example.com'
  125. end
  126. end
  127. describe '#disable_two_factor!' do
  128. it 'saves false for otp_required_for_login' do
  129. user = Fabricate.build(:user, otp_required_for_login: true)
  130. user.disable_two_factor!
  131. expect(user.reload.otp_required_for_login).to be false
  132. end
  133. it 'saves cleared otp_backup_codes' do
  134. user = Fabricate.build(:user, otp_backup_codes: %w(dummy dummy))
  135. user.disable_two_factor!
  136. expect(user.reload.otp_backup_codes.empty?).to be true
  137. end
  138. end
  139. describe '#send_confirmation_instructions' do
  140. around do |example|
  141. queue_adapter = ActiveJob::Base.queue_adapter
  142. example.run
  143. ActiveJob::Base.queue_adapter = queue_adapter
  144. end
  145. it 'delivers confirmation instructions later' do
  146. user = Fabricate(:user)
  147. ActiveJob::Base.queue_adapter = :test
  148. expect { user.send_confirmation_instructions }.to have_enqueued_job(ActionMailer::DeliveryJob)
  149. end
  150. end
  151. describe 'settings' do
  152. it 'is instance of Settings::ScopedSettings' do
  153. user = Fabricate(:user)
  154. expect(user.settings).to be_kind_of Settings::ScopedSettings
  155. end
  156. end
  157. describe '#setting_default_privacy' do
  158. it 'returns default privacy setting if user has configured' do
  159. user = Fabricate(:user)
  160. user.settings[:default_privacy] = 'unlisted'
  161. expect(user.setting_default_privacy).to eq 'unlisted'
  162. end
  163. it "returns 'private' if user has not configured default privacy setting and account is locked" do
  164. user = Fabricate(:user, account: Fabricate(:account, locked: true))
  165. expect(user.setting_default_privacy).to eq 'private'
  166. end
  167. it "returns 'public' if user has not configured default privacy setting and account is not locked" do
  168. user = Fabricate(:user, account: Fabricate(:account, locked: false))
  169. expect(user.setting_default_privacy).to eq 'public'
  170. end
  171. end
  172. describe 'whitelist' do
  173. around(:each) do |example|
  174. old_whitelist = Rails.configuration.x.email_whitelist
  175. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  176. example.run
  177. Rails.configuration.x.email_domains_whitelist = old_whitelist
  178. end
  179. it 'should not allow a user to be created unless they are whitelisted' do
  180. user = User.new(email: 'foo@example.com', account: account, password: password)
  181. expect(user.valid?).to be_falsey
  182. end
  183. it 'should allow a user to be created if they are whitelisted' do
  184. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  185. expect(user.valid?).to be_truthy
  186. end
  187. it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
  188. user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password)
  189. expect(user.valid?).to be_falsey
  190. end
  191. context do
  192. around do |example|
  193. old_blacklist = Rails.configuration.x.email_blacklist
  194. example.run
  195. Rails.configuration.x.email_domains_blacklist = old_blacklist
  196. end
  197. it 'should not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
  198. Rails.configuration.x.email_domains_blacklist = 'blacklisted.mastodon.space'
  199. user = User.new(email: 'foo@blacklisted.mastodon.space', account: account, password: password)
  200. expect(user.valid?).to be_falsey
  201. end
  202. end
  203. end
  204. it_behaves_like 'Settings-extended' do
  205. def create!
  206. User.create!(account: Fabricate(:account), email: 'foo@mastodon.space', password: 'abcd1234' )
  207. end
  208. def fabricate
  209. Fabricate(:user)
  210. end
  211. end
  212. describe 'token_for_app' do
  213. let(:user) { Fabricate(:user) }
  214. let(:app) { Fabricate(:application, owner: user) }
  215. it 'returns a token' do
  216. expect(user.token_for_app(app)).to be_a(Doorkeeper::AccessToken)
  217. end
  218. it 'persists a token' do
  219. t = user.token_for_app(app)
  220. expect(user.token_for_app(app)).to eql(t)
  221. end
  222. it 'is nil if user does not own app' do
  223. app.update!(owner: nil)
  224. expect(user.token_for_app(app)).to be_nil
  225. end
  226. end
  227. describe '#role' do
  228. it 'returns admin for admin' do
  229. user = User.new(admin: true)
  230. expect(user.role).to eq 'admin'
  231. end
  232. it 'returns moderator for moderator' do
  233. user = User.new(moderator: true)
  234. expect(user.role).to eq 'moderator'
  235. end
  236. it 'returns user otherwise' do
  237. user = User.new
  238. expect(user.role).to eq 'user'
  239. end
  240. end
  241. describe '#role?' do
  242. it 'returns false when invalid role requested' do
  243. user = User.new(admin: true)
  244. expect(user.role?('disabled')).to be false
  245. end
  246. it 'returns true when exact role match' do
  247. user = User.new
  248. mod = User.new(moderator: true)
  249. admin = User.new(admin: true)
  250. expect(user.role?('user')).to be true
  251. expect(mod.role?('moderator')).to be true
  252. expect(admin.role?('admin')).to be true
  253. end
  254. it 'returns true when role higher than needed' do
  255. mod = User.new(moderator: true)
  256. admin = User.new(admin: true)
  257. expect(mod.role?('user')).to be true
  258. expect(admin.role?('user')).to be true
  259. expect(admin.role?('moderator')).to be true
  260. end
  261. end
  262. end