Skip to content

Commit

Permalink
Clean up users fixtures and update tests
Browse files Browse the repository at this point in the history
Now that we have roles, we can target users more specifically for
specific tests. We also don’t need some of the duplicate test fixtures.

Issue #299
  • Loading branch information
reefdog committed Sep 20, 2022
1 parent df4b8d4 commit ae85c53
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 40 deletions.
14 changes: 7 additions & 7 deletions test/controllers/accounts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

test "can get the setup page with a valid token" do
user = users(:user1)
user = users(:user)

token = user.set_reset_password_token

Expand All @@ -21,7 +21,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end

test "cannot get setup page while logged in" do
user = users(:user1)
user = users(:user)

# Use a valid token to make this a legitimate test.
# The redirection will run before the token is even checked, but we want to make sure it's an
Expand All @@ -36,7 +36,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end

test "can setup an account as a new user" do
user = users(:user1)
user = users(:user)

post create_account_path({
reset_password_token: user.set_reset_password_token,
Expand All @@ -57,7 +57,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end

test "cannot setup an account with a previously-used token" do
user = users(:user1)
user = users(:user)

create_params = {
reset_password_token: user.set_reset_password_token,
Expand All @@ -76,7 +76,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end

test "cannot setup an account with blank passwords" do
user = users(:user1)
user = users(:user)

post create_account_path({
reset_password_token: user.set_reset_password_token,
Expand All @@ -87,7 +87,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end

test "cannot setup an account with short passwords" do
user = users(:user1)
user = users(:user)
short_password = "password"[0, Devise.password_length.begin - 1]

post create_account_path({
Expand All @@ -99,7 +99,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end

test "cannot setup an account with mismatching passwords" do
user = users(:user1)
user = users(:user)

post create_account_path({
reset_password_token: user.set_reset_password_token,
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/archive_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def after_all
end

test "load index if authenticated" do
sign_in users(:user1)
sign_in users(:user)
get root_url
assert_response :success
end
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/jobs_tracker_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class JobsTrackerControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

test "can view jobs" do
sign_in users(:user1)
sign_in users(:user)
InstagramMediaSource.extract("https://www.instagram.com/p/CBcqOkyDDH8/", false)
get jobs_status_index_path
assert_response :success
end

test "can resubmit a scrape" do
sign_in users(:user1)
sign_in users(:user)
scrape = Scrape.create({ fulfilled: false, url: "https://www.instagram.com/p/CBcqOkyDDH8/", scrape_type: :instagram })
assert_not_nil scrape

Expand All @@ -25,7 +25,7 @@ class JobsTrackerControllerTest < ActionDispatch::IntegrationTest
end

test "can delete a scrape" do
sign_in users(:user1)
sign_in users(:user)
scrape = Scrape.create({ fulfilled: false, url: "https://www.instagram.com/p/CBcqOkyDDH8/", scrape_type: :instagram })
assert_not_nil scrape

Expand Down
2 changes: 1 addition & 1 deletion test/controllers/text_search_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ArchiveControllerTest < ActionDispatch::IntegrationTest
Sources::Tweet.create_from_url("https://twitter.com/ggreenwald/status/1430523746457112578")
Sources::Tweet.create_from_url("https://twitter.com/bidenfoundation/status/1121446608040755200")
Sources::Tweet.create_from_url("https://twitter.com/POTUS/status/1428115295756066824")
sign_in users(:user1)
sign_in users(:user)

# And then search
get text_search_submit_url, params: { query: "Biden" }
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/api_keys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
#
one:
hashed_api_key: <%= ZenoEncryption.hash_string("123456789") %>
user: user1
user: user
22 changes: 5 additions & 17 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,13 @@
# two: {}
# column: value

user1:
email: "[email protected]"
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
confirmed_at: <%= Time.now %>

user2:
email: "[email protected]"
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
confirmed_at: <%= Time.now %>

user3:
email: "[email protected]"
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
confirmed_at: <%= Time.now %>

existing_user:
email: "[email protected]"
user:
email: "[email protected]"
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
confirmed_at: <%= Time.now %>
roles:
- insights_user
- media_vault_user

new_user:
email: "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/accounts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AccountsHelperTest < ActionView::TestCase
include Devise::Test::IntegrationHelpers

test "search_history_helper" do
sign_in users(:user1)
sign_in users(:user)
TextSearch.create(query: "search term", user: User.first)
TextSearch.create(query: "another search term", user: User.first)
TextSearch.create(query: "yet another search term", user: User.first)
Expand Down
4 changes: 2 additions & 2 deletions test/models/api_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

class ApiKeyTest < ActiveSupport::TestCase
test "creating an api key returns the clear string of the key" do
api_key = ApiKey.create(user: users(:user1))
api_key = ApiKey.create(user: users(:user))
assert_not_nil api_key.api_key
assert_not_nil api_key.hashed_api_key
end

test "retrieving an api key does not return the clear string of the key" do
api_key = ApiKey.create(user: users(:user1))
api_key = ApiKey.create(user: users(:user))
api_key = ApiKey.find(api_key.id)
assert_nil api_key.api_key
assert_not_nil api_key.hashed_api_key
Expand Down
4 changes: 2 additions & 2 deletions test/models/image_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ImageSearchTest < ActiveSupport::TestCase
def setup
image_file = File.open("test/mocks/media/instagram_media_12765281-136d-4bfa-b7ad-e89f107b5769.jpg", binmode: true)
video_file = File.open("test/mocks/media/youtube_media_23b12624-2ef2-4dcb-97d2-966aa9fcba80.mp4", binmode: true)
@image_search = ImageSearch.create!(image: image_file, user: users(:user1))
@video_search = ImageSearch.create!(video: video_file, user: users(:user1))
@image_search = ImageSearch.create!(image: image_file, user: users(:user))
@video_search = ImageSearch.create!(video: video_file, user: users(:user))

Zelkova.graph.reset
end
Expand Down
10 changes: 5 additions & 5 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class UserTest < ActiveSupport::TestCase
end

test "should recognize users are not admins" do
assert_not users(:existing_user).is_admin?
assert_not users(:user).is_admin?
end

test "can associate a user with an applicant" do
user = users(:user1)
user = users(:user)
applicant = applicants(:approved)

user.update(applicant: applicant)
Expand All @@ -42,7 +42,7 @@ class UserTest < ActiveSupport::TestCase
end

test "destroying user destroys applicant too" do
user = users(:user1)
user = users(:user)
applicant = applicants(:approved)

user.update(applicant: applicant)
Expand Down Expand Up @@ -82,14 +82,14 @@ class UserTest < ActiveSupport::TestCase
end

test "can create a reset password token" do
user = users(:user1)
user = users(:user)
token = user.set_reset_password_token

assert token
end

test "can look up a user with the reset token" do
user = users(:user1)
user = users(:user)
token = user.set_reset_password_token

assert_equal user, User.with_reset_password_token(token)
Expand Down

0 comments on commit ae85c53

Please sign in to comment.