heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
23.92k stars 5.54k forks source link

Default fxtures violate UNIQUE constraint #5498

Open gardnerapp opened 2 years ago

gardnerapp commented 2 years ago

Environment:

ruby 3.1.1 rails 7.0.3 devise 4.8.1

Reproduce: rails g devise:install && rails g devise user rails test

Result: All test fail because user fixtures fail unique constraint ActiveRecord::RecordNotUnique: RuntimeError: UNIQUE constraint failed: users.email fixtures: one: {} two: {}

Expected: Test run without error, fixtures generated with different emails

one:
      email: foo@example.com

two:
     email: bar@example.com
gardnerapp commented 2 years ago

Which part of Devise is responsible for generating fixtures? Could just run a system call with echo towards the fixture file but I feel like that is cheating.

carlosantoniodasilva commented 2 years ago

Devise just uses the Rails model generator for that: https://github.com/heartcombo/devise/blob/f8d1ea90bc328012f178b8a6616a89b73f2546a4/lib/generators/active_record/devise_generator.rb#L25

You technically get the same output if you just generate a User model yourself with Rails:

% rails g model user
      invoke  active_record
      create    db/migrate/20220609144317_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

% cat test/fixtures/users.yml
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

While we could technically replace the fixture with one of our own, I'm not sure there's much gain to it, if anyone is going to use the fixture they will likely be editing it with other values anyway.

ftambara commented 1 year ago

I could imagine better default fixtures / a documented template being useful for quick reference. Especially to see how to build a working fixture when we configure multiple modules (like confirmable). That's what brought me to this issue anyways.

gardnerapp commented 1 year ago

Normally when I generate a rails model it will populate the fixture with data: rails g model Foo bar bang invoke active_record create db/migrate/20221029225445_create_foos.rb create app/models/foo.rb invoke test_unit create test/models/foo_test.rb create test/fixtures/foos.yml

` cat test/fixtures/foo.yml one: bar: MyString bang: MyString

two: bar: MyString bang: MyString`

Not sure why the mock up values are not present for the Devise fixture. What we could do is create email fixtures with dummy emails example1@example.com and example2@example.com. Any suggestions for where to start?

AllanSiqueira commented 3 months ago

I think this was fixed already right? This issue can be closed