Class: Waylon::RSpec::TestUser

Inherits:
Object
  • Object
show all
Includes:
User
Defined in:
lib/waylon/rspec/test_user.rb

Overview

Extras for RSpec to facilitate testing Waylon (by creating fake Users)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from User

#<=>, included

Constructor Details

#initialize(user_id, details = {}) ⇒ TestUser

Returns a new instance of TestUser.

Parameters:

  • user_id (Integer)

    The ID of the user in the TestSense’s user list

  • details (Hash) (defaults to: {})

    Optional User details (can be looked up later)



88
89
90
91
# File 'lib/waylon/rspec/test_user.rb', line 88

def initialize(user_id, details = {})
  @id = user_id.to_i
  @details = details
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/waylon/rspec/test_user.rb', line 9

def id
  @id
end

Class Method Details

.email_from_name(name) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an email address based on the name provided

Returns:

  • (String)

    A generated email address



149
150
151
152
153
154
155
# File 'lib/waylon/rspec/test_user.rb', line 149

def self.email_from_name(name)
  if ENV.fetch("USER_EMAIL", nil) && name == "homer.simpson"
    ENV.fetch("USER_EMAIL", nil)
  else
    "#{name.downcase.gsub(/[\s_-]/, ".")}@example.com"
  end
end

.find_by_email(email) ⇒ User?

Looks up a User by their email

Parameters:

  • email (String)

    The User’s email

Returns:

  • (User, nil)

    The found User



14
15
16
# File 'lib/waylon/rspec/test_user.rb', line 14

def self.find_by_email(email)
  TestSense.user_list.find { |user| user.email == email }
end

.find_by_handle(handle) ⇒ User?

Looks up a User by their IM handle

Parameters:

  • handle (String)

    The User’s handle

Returns:

  • (User, nil)

    The found User



21
22
23
# File 'lib/waylon/rspec/test_user.rb', line 21

def self.find_by_handle(handle)
  TestSense.user_list.find { |user| user.handle == handle }
end

.find_by_name(name) ⇒ User?

Looks up a User by their full name

Parameters:

  • name (String)

    The User’s name

Returns:

  • (User, nil)

    The found User



28
29
30
# File 'lib/waylon/rspec/test_user.rb', line 28

def self.find_by_name(name)
  TestSense.user_list.find { |user| user.display_name == name }
end

.find_or_create(name: nil, email: nil, handle: nil) ⇒ User, Boolean

Looks up existing or creates a new User based on their full name, email, or handle rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity

Parameters:

  • name (String) (defaults to: nil)

    The full name of the User

  • email (String) (defaults to: nil)

    The User’s email

  • handle (String) (defaults to: nil)

    The User’s handle

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/waylon/rspec/test_user.rb', line 38

def self.find_or_create(name: nil, email: nil, handle: nil)
  return false unless name || email || handle # have to provide _something_

  existing_user = find_by_email(email) || find_by_handle(handle) || find_by_name(name)
  if existing_user
    existing_user
  else
    this_name = name || random_name # if no name was provided, make one up
    details = {
      email: email || email_from_name(this_name),
      handle: handle || handle_from_name(this_name),
      name: this_name,
      status: :online
    }
    # Need to give up if we've generated a duplicate
    if find_by_email(details[:email]) || find_by_handle(details[:handle]) || find_by_name(details[:name])
      return false
    end

    TestSense.add_user_from_details(details)
  end
end

.handle_from_name(name) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a handle from a name

Returns:

  • (String)

    A generated user handle



160
161
162
# File 'lib/waylon/rspec/test_user.rb', line 160

def self.handle_from_name(name)
  name.downcase.split(/[\s_-]/).first
end

.random_nameString

Provides a random human-sounding full name for test users

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/waylon/rspec/test_user.rb', line 64

def self.random_name
  first_names = %w[
    Abraham Al Alex Barbara Barry Bob Brenda Chloe Chuck Daniel Dave Eliza Felicia Frank
    Francis Glen Graham Greg Hal Jackie Jacob Jessica Jonathan Julie Maria Marcia Nikhil
    Olivia Patrick Paul Reggie Robby Roger Sam Saul Sean Tim Todd Tristan Xavier Zack
  ]
  last_names = %w[
    Adams Andrews Bailey Brooks Brown Bush Cervantes Chen Collins Crooks Dean Franz Harris
    Jackson Jimenez Jones Jordan Laflor Lopez Gonzalez McDowell Miller Ng Odinson Reed
    Roberts Rodriguez Sanders Schmidt Scott Smith Stewart Taylor Tesla Torres Turner
    Walker Ward Warner White Williams Wilson Wong Young Zeta Zimmerman
  ]

  "#{first_names.sample} #{last_names.sample}"
end

.whoamiTestUser

Gives back the TestUser for the bot

Returns:

  • (TestUser)

    Waylon’s User instance



82
83
84
# File 'lib/waylon/rspec/test_user.rb', line 82

def self.whoami
  find_by_email("[email protected]")
end

Instance Method Details

#detailsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lazily provides the details for a TestUser

Returns:

  • (Hash)

    Details for this instance



141
142
143
144
# File 'lib/waylon/rspec/test_user.rb', line 141

def details
  @details = TestSense.user_list[id].details if @details.empty?
  @details.dup
end

#display_nameString

The User’s full name (:user from the details Hash)

Returns:

  • (String)


95
96
97
# File 'lib/waylon/rspec/test_user.rb', line 95

def display_name
  details[:name]
end

#emailString

The User’s email address

Returns:

  • (String)


101
102
103
# File 'lib/waylon/rspec/test_user.rb', line 101

def email
  details[:email]
end

#handleString

The User’s handle

Returns:

  • (String)


122
123
124
# File 'lib/waylon/rspec/test_user.rb', line 122

def handle
  details[:handle]
end

#private_message(content) ⇒ TestMessage

Sends a direct TestMessage to a User

Parameters:

  • content (String)

    The message content to send

Returns:



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/waylon/rspec/test_user.rb', line 108

def private_message(content)
  msg = {
    user_id: self.class.whoami.id,
    receiver_id: id,
    text: content,
    type: :private,
    created_at: Time.now
  }
  TestSense.message_list << msg
  TestMessage.new(TestSense.message_list.size - 1)
end

#statusSymbol

The User’s current status

Returns:

  • (Symbol)


128
129
130
# File 'lib/waylon/rspec/test_user.rb', line 128

def status
  details[:status]
end

#valid?Boolean

Is the User valid?

Returns:

  • (Boolean)


134
135
136
# File 'lib/waylon/rspec/test_user.rb', line 134

def valid?
  true
end