summaryrefslogtreecommitdiff
path: root/test/models/user_test.rb
blob: 858cacb967592bcaa16f5ab4f487821e8a2837dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true

# == Schema Information
#
# Table name: users
#
#  id                              :integer          not null, primary key
#  anonymous_by_default            :boolean          default(FALSE), not null
#  authentication_token            :string           not null
#  avatar                          :string
#  comments_always_jump_to_last    :boolean          default(FALSE), not null
#  comments_newest_first           :boolean          default(TRUE), not null
#  comments_per_page               :integer          default(20), not null
#  comments_posted_count           :integer          default(0), not null
#  consumed_timestep               :integer
#  current_sign_in_at              :datetime
#  current_sign_in_ip              :inet
#  deleted_at                      :datetime
#  description                     :string
#  email                           :string           default(""), not null
#  encrypted_password              :string           default(""), not null
#  failed_attempts                 :integer
#  fancy_tag_field_on_edit         :boolean          default(TRUE), not null
#  fancy_tag_field_on_upload       :boolean          default(TRUE), not null
#  forum_posts_count               :integer          default(0), not null
#  hide_comments                   :boolean          default(FALSE)
#  hide_default_role               :boolean          default(FALSE), not null
#  hide_vote_counts                :boolean          default(FALSE), not null
#  images_per_page                 :integer          default(15), not null
#  last_renamed_at                 :datetime         default(Thu, 01 Jan 1970 00:00:00.000000000 UTC +00:00), not null
#  last_sign_in_at                 :datetime
#  last_sign_in_ip                 :inet
#  locked_at                       :datetime
#  messages_newest_first           :boolean          default(FALSE), not null
#  metadata_updates_count          :integer          default(0), not null
#  name                            :string           not null
#  no_spoilered_in_watched         :boolean          default(FALSE), not null
#  otp_backup_codes                :string           is an Array
#  otp_required_for_login          :boolean
#  otp_secret                      :text
#  personal_title                  :string
#  posts_favorited_count           :integer          default(0), not null
#  recent_filter_ids               :integer          default([]), not null, is an Array
#  remember_created_at             :datetime
#  reset_password_sent_at          :datetime
#  reset_password_token            :string
#  role                            :string           default("user"), not null
#  scale_large_images              :boolean          default(TRUE), not null
#  scratchpad                      :text
#  secondary_role                  :string
#  serve_webm                      :boolean          default(FALSE), not null
#  show_large_thumbnails           :boolean          default(TRUE), not null
#  show_sidebar_and_watched_images :boolean          default(TRUE), not null
#  sign_in_count                   :integer          default(0), not null
#  slug                            :string           not null
#  spoiler_type                    :string           default("static"), not null
#  theme                           :string           default("default"), not null
#  topic_count                     :integer          default(0), not null
#  unlock_token                    :string
#  uploads_count                   :integer          default(0), not null
#  use_centered_layout             :boolean          default(FALSE), not null
#  votes_cast_count                :integer          default(0), not null
#  watch_on_new_topic              :boolean          default(TRUE), not null
#  watch_on_reply                  :boolean          default(TRUE), not null
#  watch_on_upload                 :boolean          default(TRUE), not null
#  watched_images_exclude_str      :string           default(""), not null
#  watched_images_query_str        :string           default(""), not null
#  watched_tag_ids                 :integer          default([]), not null, is an Array
#  created_at                      :datetime         not null
#  updated_at                      :datetime         not null
#  current_filter_id               :integer
#  deleted_by_user_id              :integer
#
# Indexes
#
#  index_users_on_authentication_token  (authentication_token) UNIQUE
#  index_users_on_current_filter_id     (current_filter_id)
#  index_users_on_deleted_by_user_id    (deleted_by_user_id) WHERE (deleted_by_user_id IS NOT NULL)
#  index_users_on_email                 (email) UNIQUE
#  index_users_on_name                  (name)
#  index_users_on_reset_password_token  (reset_password_token) UNIQUE
#  index_users_on_slug                  (slug)
#
# Foreign Keys
#
#  fk_rails_...  (current_filter_id => filters.id) ON DELETE => restrict ON UPDATE => cascade
#  fk_rails_...  (deleted_by_user_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  setup do
    @user = build(:user)
  end

  test 'should be valid' do
    @user.valid?
  end

  test 'should have default filter after creation' do
    @user.save!
    assert_equal 'Default', @user.current_filter.name
  end

  test 'switching filters should populate recent_filter_ids without duplicates' do
    @filter1 = create(:filter_everything)
    @filter2 = create(:filter_porn_only)

    assert_not_includes @user.recent_filter_ids, @filter1.id
    @user.set_filter!(@filter1)
    assert_includes @user.recent_filter_ids, @filter1.id

    @user.set_filter!(@filter2)
    @user.set_filter!(@filter1)
    @user.set_filter!(@filter2)
    assert_equal 1, @user.recent_filter_ids.count(@filter1.id)
    assert_equal 1, @user.recent_filter_ids.count(@filter2.id)
  end

  test '#staff?' do
    assert_not build(:user).staff?
    assert build(:assistant).staff?
    assert build(:moderator).staff?
    assert build(:admin).staff?
  end
end