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

MarkUsProject / Markus / 26985068800

04 Jun 2026 11:09PM UTC coverage: 84.913% (-5.3%) from 90.19%
26985068800

Pull #7972

github

web-flow
Merge c74925818 into 9a5124c61
Pull Request #7972: Parallelize rspec tests

1023 of 2226 branches covered (45.96%)

Branch coverage included in aggregate %.

36935 of 42476 relevant lines covered (86.95%)

113.68 hits per line

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

18.64
/app/controllers/api/users_controller.rb
1
module Api
1✔
2
  # Allows for adding, modifying and showing Markus users.
3
  # Uses Rails' RESTful routes (check 'rake routes' for the configured routes)
4
  class UsersController < MainApiController
1✔
5
    # Define default fields to display for index and show methods
6
    DEFAULT_FIELDS = [:id, :user_name, :email, :id_number, :type, :first_name, :last_name].freeze
1✔
7

8
    # Returns users and their attributes
9
    # Optional: filter, fields
10
    def index
1✔
11
      users = get_collection(visible_users) || return
×
12

13
      respond_to do |format|
×
14
        format.xml { render xml: users.to_xml(only: DEFAULT_FIELDS, root: :users, skip_types: true) }
×
15
        format.json do
×
16
          render json: users.pluck_to_hash(*DEFAULT_FIELDS)
×
17
        end
18
      end
19
    end
20

21
    # Creates a new user
22
    # Requires: user_name, type, first_name, last_name
23
    # Optional: section_name, grace_credits
24
    def create
1✔
25
      if has_missing_params?([:user_name, :type, :first_name, :last_name])
×
26
        # incomplete/invalid HTTP params
27
        render 'shared/http_status', locals: { code: '422', message:
×
28
          HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_content
29
        return
×
30
      end
31

32
      # Check if that user_name is taken
33
      user = User.find_by(user_name: params[:user_name])
×
34
      unless user.nil?
×
35
        render 'shared/http_status', locals: { code: '409', message:
×
36
          'User already exists' }, status: :conflict
37
        return
×
38
      end
39

40
      # No conflict found, so create new user
41
      param_user_type = params[:type].camelize.downcase
×
42
      params.delete(:type)
×
43

44
      begin
45
        case param_user_type
×
46
        when 'enduser'
47
          EndUser.create!(params.permit(*DEFAULT_FIELDS))
×
48
        when 'adminuser'
49
          AdminUser.create!(params.permit(*DEFAULT_FIELDS))
×
50
        else
51
          render 'shared/http_status', locals: { code: '422', message: 'Unknown user type' },
×
52
                                       status: :unprocessable_content
53
          return
×
54
        end
55
      rescue ActiveRecord::SubclassNotFound, ActiveRecord::RecordInvalid => e
56
        render 'shared/http_status', locals: { code: '422', message: e.to_s }, status: :unprocessable_content
×
57
      else
58
        render 'shared/http_status',
×
59
               locals: { code: '201', message: HttpStatusHelper::ERROR_CODE['message']['201'] }, status: :created
60
      end
61
    end
62

63
    # Returns a user and its attributes
64
    # Requires: id
65
    # Optional: filter, fields
66
    def show
1✔
67
      user = visible_users.find_by(id: params[:id])
×
68
      if user.nil?
×
69
        # No user with that id
70
        render 'shared/http_status', locals: { code: '404', message:
×
71
          'No user exists with that id' }, status: :not_found
72
      else
73
        respond_to do |format|
×
74
          format.xml { render xml: user.to_xml(only: DEFAULT_FIELDS, root: :user, skip_types: true) }
×
75
          format.json { render json: user.to_json(only: DEFAULT_FIELDS) }
×
76
        end
77
      end
78
    end
79

80
    # Requires: id
81
    # Optional: first_name, last_name, user_name
82
    def update
1✔
83
      user = visible_users.find_by(id: params[:id])
×
84
      if user.nil?
×
85
        render 'shared/http_status', locals: { code: '404', message: 'User was not found' }, status: :not_found
×
86
        return
×
87
      end
88
      user.update!(user_params)
×
89
    rescue ActiveRecord::SubclassNotFound, ActiveRecord::RecordInvalid => e
90
      render 'shared/http_status', locals: { code: '422', message: e.to_s }, status: :unprocessable_content
×
91
    rescue StandardError
92
      render 'shared/http_status', locals: { code: '500', message:
×
93
        HttpStatusHelper::ERROR_CODE['message']['500'] }, status: :internal_server_error
94
    else
95
      render 'shared/http_status', locals: { code: '200', message:
×
96
        HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
97
    end
98

99
    # Update a user's attributes based on their user_name as opposed
100
    # to their id (use the regular update method instead)
101
    # Requires: user_name
102
    def update_by_username
1✔
103
      if has_missing_params?([:user_name])
×
104
        # incomplete/invalid HTTP params
105
        render 'shared/http_status',
×
106
               locals: { code: '422', message: HttpStatusHelper::ERROR_CODE['message']['422'] },
107
               status: :unprocessable_content
108
        return
×
109
      end
110

111
      user = User.find_by(user_name: params[:user_name])
×
112
      if user.nil?
×
113
        render 'shared/http_status', locals: { code: '404', message: 'User was not found' }, status: :not_found
×
114
        return
×
115
      end
116
      user.update!(user_params)
×
117
    rescue ActiveRecord::SubclassNotFound, ActiveRecord::RecordInvalid => e
118
      render 'shared/http_status', locals: { code: '422', message: e.to_s }, status: :unprocessable_content
×
119
    rescue StandardError
120
      render 'shared/http_status', locals: { code: '500', message:
×
121
        HttpStatusHelper::ERROR_CODE['message']['500'] }, status: :internal_server_error
122
    else
123
      render 'shared/http_status', locals: { code: '200', message:
×
124
        HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
125
    end
126

127
    private
1✔
128

129
    # Do not make AutotestUser users visible
130
    def visible_users
1✔
131
      User.where.not(type: :AutotestUser)
×
132
    end
133

134
    def user_params
1✔
135
      params.permit(:user_name, :email, :id_number, :first_name, :last_name)
×
136
    end
137
  end
138
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

© 2026 Coveralls, Inc