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

MarkUsProject / Markus / 20243384039

11 Dec 2025 06:21PM UTC coverage: 91.513%. First build
20243384039

Pull #7763

github

web-flow
Merge 9f55e660a into 0d3f24005
Pull Request #7763: Release 2.9.0

914 of 1805 branches covered (50.64%)

Branch coverage included in aggregate %.

1584 of 1666 new or added lines in 108 files covered. (95.08%)

43650 of 46892 relevant lines covered (93.09%)

121.64 hits per line

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

77.59
/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
8✔
12

13
      respond_to do |format|
8✔
14
        format.xml { render xml: users.to_xml(only: DEFAULT_FIELDS, root: :users, skip_types: true) }
12✔
15
        format.json { render json: users.to_json(only: DEFAULT_FIELDS) }
12✔
16
      end
17
    end
18

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

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

38
      # No conflict found, so create new user
39
      param_user_type = params[:type].camelize.downcase
6✔
40
      params.delete(:type)
6✔
41

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

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

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

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

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

125
    private
1✔
126

127
    # Do not make AutotestUser users visible
128
    def visible_users
1✔
129
      User.where.not(type: :AutotestUser)
17✔
130
    end
131

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