• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / tigerdata-app / 5ac3f711-df4d-4e61-ac8c-b3084a6229b9

15 Nov 2024 02:46PM UTC coverage: 84.306% (+0.04%) from 84.27%
5ac3f711-df4d-4e61-ac8c-b3084a6229b9

Pull #1035

circleci

hectorcorrea
Simplify a couple of tests
Pull Request #1035: Started switching user data entry to use autocomplete

4 of 4 branches covered (100.0%)

15 of 16 new or added lines in 2 files covered. (93.75%)

2181 of 2587 relevant lines covered (84.31%)

300.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.06
/app/models/user.rb
1
# frozen_string_literal: true
2

3
require "csv"
1✔
4
class User < ApplicationRecord
1✔
5
  # Include default devise modules. Others available are:
6
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
7
  devise :rememberable, :omniauthable
1✔
8

9
  has_many :user_requests, dependent: :destroy
1✔
10

11
  USER_REGISTRATION_LIST = Rails.root.join("data", "user_registration_list_#{Rails.env}.csv")
1✔
12

13
  def self.from_cas(access_token)
1✔
14
    user = User.find_by(provider: access_token.provider, uid: access_token.uid)
9✔
15
    if user.present? && user.given_name.nil? # fix any users that do not have the name information loaded
9✔
16
      user.initialize_name_values(access_token.extra)
7✔
17
      user.save
7✔
18
    end
19
    user
9✔
20
  end
21

22
  # Users that can be project sponsors
23
  def self.sponsor_users
1✔
24
    if Rails.env.development? || Rails.env.staging?
27✔
25
      User.where(eligible_sponsor: true).or(User.where(superuser: true))
1✔
26
    else
27
      User.where(eligible_sponsor: true)
26✔
28
    end
29
  end
30

31
  # Users that can be data managers
32
  def self.manager_users
1✔
33
    if Rails.env.development? || Rails.env.staging?
25✔
NEW
34
      User.where(eligible_manager: true).or(User.where(superuser: true))
×
35
    else
36
      User.where(eligible_manager: true)
25✔
37
    end
38
  end
39

40
  def clear_mediaflux_session(session)
1✔
41
    @mediaflux_session = nil
4✔
42
    session[:mediaflux_session] = nil
4✔
43
  end
44

45
  def mediaflux_from_session(session)
1✔
46
    if session[:mediaflux_session].blank?
4✔
47
      session[:mediaflux_session] = mediaflux_session
4✔
48
    else
49
      @mediaflux_session = session[:mediaflux_session]
×
50
    end
51
  end
52

53
  def mediaflux_session
1✔
54
    @mediaflux_session ||= begin
919✔
55
                            logon_request = Mediaflux::LogonRequest.new
360✔
56
                            logon_request.session_token
360✔
57
                          end
58
  end
59

60
  def terminate_mediaflux_session
1✔
61
    return if @mediaflux_session.nil? # nothing to terminate
×
62

63
    Mediaflux::LogoutRequest.new(session_token: @mediaflux_session).response_body
×
64
    @mediaflux_session = nil
×
65
  end
66

67
  # Initialize the name values from the CAS information.
68
  # Our name fields do not match their name fields, so we need to translate.
69
  def initialize_name_values(extra_cas_info)
1✔
70
    self.given_name = extra_cas_info.givenname
7✔
71
    self.family_name =  extra_cas_info.sn
7✔
72
    self.display_name = extra_cas_info.pudisplayname
7✔
73
  end
74

75
  # Return the display name if it exists, otherwise return the uid
76
  # @return [String]
77
  def display_name_safe
1✔
78
    return uid if display_name.blank?
455✔
79

80
    display_name
453✔
81
  end
82

83
  # Is this user eligible to be a data sponsor in this environment?
84
  # @return [Boolean]
85
  def eligible_sponsor?
1✔
86
    return true if superuser
211✔
87
    super
196✔
88
  end
89

90
  # Is this user eligible to be a data manger in this environment?
91
  # @return [Boolean]
92
  def eligible_manager?
1✔
93
    return true if superuser
84✔
94
    super
80✔
95
  end
96

97
  # Is this user eligible to be a data user in this environment?
98
  # @return [Boolean]
99
  def eligible_data_user?
1✔
100
    return true if superuser
10✔
101
    return true if !eligible_sponsor? && !eligible_manager
10✔
102
  end
103

104
  # Is this user eligible to be a sysadmin in this environment?
105
  # @return [Boolean]
106
  def eligible_sysadmin?
1✔
107
    return true if superuser || sysadmin
312✔
108
  end
109

110
  # Parse the USER_REGISTRATION_LIST csv
111
  # @return [CSV::Table]
112
  def self.csv_data
1✔
113
    CSV.parse(File.read(USER_REGISTRATION_LIST), headers: true)
6✔
114
  end
115

116
  # Load the user registration list from the CSV file.
117
  # Select the file that matches the rails environment.
118
  def self.load_registration_list
1✔
119
    User.csv_data.each do |line|
7✔
120
      user = User.find_by(uid: line["uid"]) || User.new
276✔
121
      user.uid = line["uid"]
276✔
122
      user.family_name = line["family_name"]
276✔
123
      user.display_name = line["display_name"]
276✔
124
      user.email = user.uid + "@princeton.edu"
276✔
125
      # If we don't say that this is a cas user, they won't be able to log in with CAS
126
      user.provider = "cas"
276✔
127
      user.eligible_sponsor = line["eligible_sponsor"] == "TRUE"
276✔
128
      user.eligible_manager = line["eligible_manager"] == "TRUE"
276✔
129
      user.superuser = line["superuser"] == "TRUE"
276✔
130
      user.sysadmin = line["sysadmin"] == "TRUE"
276✔
131
      user.trainer = line["tester_trainer"] == "TRUE"
276✔
132
      user.save
276✔
133
    end
134
  end
135

136
  # Methods serialize_into_session() and serialize_from_session() are called by Warden/Devise
137
  # to calculate what information will be stored in the session and to serialize an object
138
  # back from the session.
139
  #
140
  # By default Warden/Devise store the database ID of the record (e.g. User.id) but this causes
141
  # problems if we repopulate our User table and the IDs change. The implementation provided below
142
  # uses the User.uid field (which is unique, does not change, and it's required) as the value to
143
  # store in the session to prevent this issue.
144
  #
145
  # References:
146
  #   https://stackoverflow.com/questions/23597718/what-is-the-warden-data-in-a-rails-devise-session-composed-of/23683925#23683925
147
  #   https://web.archive.org/web/20211028103224/https://tadas-s.github.io/ruby-on-rails/2020/08/02/devise-serialize-into-session-trick/
148
  #   https://github.com/wardencommunity/warden/wiki/Setup
149
  def self.serialize_into_session(record)
1✔
150
    # The return value _must_ have at least two elements since the serialize_from_session() requires
151
    # two arguments (see below)
152
    [record.uid, ""]
139✔
153
  end
154

155
  def self.serialize_from_session(key, _salt, _opts = {})
1✔
156
    User.where(uid: key)&.first
129✔
157
  end
158
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc