Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random Test Failure: Faker::Omniauth tests fail when generating a name with an apostrophe #2943

Open
alextaujenis opened this issue Apr 22, 2024 · 3 comments

Comments

@alextaujenis
Copy link
Contributor

Describe The Bug

The Faker::Omniauth module sometimes initializes a last name with an apostrophe (such as O'Connell), which will generate an email address that fails the validation tests within TestFakerInternetOmniauth.

To Reproduce

With Faker v3.3.1:

  1. Locate the test file test/faker/default/test_faker_omniauth.rb.
  2. Provide a name with an apostrophe such as: Faker::Omniauth.linkedin(name: "Alexis O'Connell")
  3. Validate the returned omniauth email address with def email_regex(first_name, last_name) (found within the test file, also below in the reproduction script).

Use the reproduction script below to reproduce the issue:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'
  gem "minitest"
end

require "minitest/autorun"

class TestFakerInternetOmniauth < Minitest::Test
  def test_omniauth_linkedin
    @tester = Faker::Omniauth
    auth = @tester.linkedin(name: "Alexis O'Connell")
    info = auth[:info]
    first_name = info[:first_name].downcase
    last_name = info[:last_name].downcase

    assert_match email_regex(first_name, last_name), info[:email]
  end

  def email_regex(first_name, last_name)
    /(#{first_name}(.|_)#{last_name}|#{last_name}(.|_)#{first_name})@(.*).(example|test)/i
  end
end

Expected Behavior

The TestFakerInternetOmniauth tests should pass when generating an omniauth profile that includes an apostrophe in the name.

Additional Context

This email validation issue was mentioned in PR #2940 (review).

@keshavbiswa
Copy link
Contributor

So currently when we run

Faker::Internet.email(name: "Alexis O'MConnell")

#=> o_alexis_mconnell@zieme.example

This might not be what we expect. But I'm not sure what is the correct representation for this?

@keshavbiswa
Copy link
Contributor

Is it o.connell_alexis@zieme.example?

@alextaujenis
Copy link
Contributor Author

alextaujenis commented Apr 26, 2024

@keshavbiswa exactly...

So this generator will return all of these possible permutations of the username:

alexis.o.connell@domain.example
alexis.connell.o@domain.example
connell.alexis.o@domain.example
connell.o.alexis@domain.example
o.connell.alexis@domain.example
o.alexis.connell@domain.example
alexis_o_connell@domain.example
alexis_connell_o@domain.example
connell_alexis_o@domain.example
connell_o_alexis@domain.example
o_connell_alexis@domain.example
o_alexis_connell@domain.example

and the test regex is only validating these possibilities:

alexis.o'connell@domain.example
o'connell.alexis@domain.example
alexis_o'connell@domain.example
o'connell_alexis@domain.example

Here is the relevant code that generates these values within Faker::Internet.email (code edited below for brevity):

  def email(name: nil, separators: nil, domain: nil)
    local_part = username(specifier: name, separators: separators)
    generate_domain = domain_name(domain: domain)
    return [local_part, generate_domain].join('@')
  end

  def username(specifier: nil, separators: %w[. _])
    return shuffle(specifier.scan(/[[:word:]]+/)).join(sample(separators)).downcase
  end

What's happening here is that Faker::Internet.email will accept a name and split the string attempting to find the different parts, shuffle the different parts, then join them back together with a random separator of a period or underscore. The issue is the apostrophe in O'Connell being treated as a word separator and the O is being independently shuffled as part of the username.

This issue has what appears to be several cascading problems that could be solved different ways:

  1. An apostrophe is valid within an email address, so this generator could be adjusted to keep O'Connell together and not shuffle the O within the username, then adjust the email_regex to accommodate. This would allow the test to still verify the provided username and returned email address without stripping the apostrophe.

  2. The email_regex could be adjusted to just verify ANY valid email address instead of all possible first.last@domain.example permutations. However, this would reduce the test suites ability to detect the expected outcome of including the user's full name within the generated email address.

  3. Strip the apostrophe from the last name within the test suite and just return permutations of alexis.oconnell@domain.example. This will still shuffle last names with an apostrophe when they are provided in real-world scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants