Commit 710a192a authored by Michael Leopard's avatar Michael Leopard Committed by Ash McKenzie

Exposing more user preferences in the API

Updating tests. Validating newly exposed parameters
parent 789ca265
...@@ -190,6 +190,12 @@ class User < ApplicationRecord ...@@ -190,6 +190,12 @@ class User < ApplicationRecord
validate :owns_commit_email, if: :commit_email_changed? validate :owns_commit_email, if: :commit_email_changed?
validate :signup_domain_valid?, on: :create, if: ->(user) { !user.created_by_id } validate :signup_domain_valid?, on: :create, if: ->(user) { !user.created_by_id }
validates :theme_id, allow_nil: true, inclusion: { in: Gitlab::Themes.valid_ids,
message: _("%{placeholder} is not a valid theme") % { placeholder: '%{value}' } }
validates :color_scheme_id, allow_nil: true, inclusion: { in: Gitlab::ColorSchemes.valid_ids,
message: _("%{placeholder} is not a valid color scheme") % { placeholder: '%{value}' } }
before_validation :sanitize_attrs before_validation :sanitize_attrs
before_validation :set_notification_email, if: :new_record? before_validation :set_notification_email, if: :new_record?
before_validation :set_public_email, if: :public_email_changed? before_validation :set_public_email, if: :public_email_changed?
......
---
title: Expose theme and color scheme user preferences in API
merge_request: 24409
author:
type: changed
...@@ -385,6 +385,8 @@ Parameters: ...@@ -385,6 +385,8 @@ Parameters:
- `skip_confirmation` (optional) - Skip confirmation - true or false (default) - `skip_confirmation` (optional) - Skip confirmation - true or false (default)
- `external` (optional) - Flags the user as external - true or false (default) - `external` (optional) - Flags the user as external - true or false (default)
- `avatar` (optional) - Image file for user's avatar - `avatar` (optional) - Image file for user's avatar
- `theme_id` (optional) - The GitLab theme for the user (see [the user preference docs](../user/profile/preferences.md#navigation-theme) for more information)
- `color_scheme_id` (optional) - User's color scheme for the file viewer (see [the user preference docs](../user/profile/preferences.md#syntax-highlighting-theme) for more information)
- `private_profile` (optional) - User's profile is private - true, false (default), or null (will be converted to false) - `private_profile` (optional) - User's profile is private - true, false (default), or null (will be converted to false)
- `shared_runners_minutes_limit` (optional) - Pipeline minutes quota for this user **(STARTER)** - `shared_runners_minutes_limit` (optional) - Pipeline minutes quota for this user **(STARTER)**
- `extra_shared_runners_minutes_limit` (optional) - Extra pipeline minutes quota for this user **(STARTER)** - `extra_shared_runners_minutes_limit` (optional) - Extra pipeline minutes quota for this user **(STARTER)**
...@@ -423,6 +425,8 @@ Parameters: ...@@ -423,6 +425,8 @@ Parameters:
- `shared_runners_minutes_limit` (optional) - Pipeline minutes quota for this user - `shared_runners_minutes_limit` (optional) - Pipeline minutes quota for this user
- `extra_shared_runners_minutes_limit` (optional) - Extra pipeline minutes quota for this user - `extra_shared_runners_minutes_limit` (optional) - Extra pipeline minutes quota for this user
- `avatar` (optional) - Image file for user's avatar - `avatar` (optional) - Image file for user's avatar
- `theme_id` (optional) - The GitLab theme for the user (see [the user preference docs](../user/profile/preferences.md#navigation-theme) for more information)
- `color_scheme_id` (optional) - User's color scheme for the file viewer (see [the user preference docs](../user/profile/preferences.md#syntax-highlighting-theme) for more information)
- `private_profile` (optional) - User's profile is private - true, false (default), or null (will be converted to false) - `private_profile` (optional) - User's profile is private - true, false (default), or null (will be converted to false)
- `shared_runners_minutes_limit` (optional) - Pipeline minutes quota for this user **(STARTER)** - `shared_runners_minutes_limit` (optional) - Pipeline minutes quota for this user **(STARTER)**
- `extra_shared_runners_minutes_limit` (optional) - Extra pipeline minutes quota for this user **(STARTER)** - `extra_shared_runners_minutes_limit` (optional) - Extra pipeline minutes quota for this user **(STARTER)**
......
...@@ -52,6 +52,8 @@ module API ...@@ -52,6 +52,8 @@ module API
optional :external, type: Boolean, desc: 'Flag indicating the user is an external user' optional :external, type: Boolean, desc: 'Flag indicating the user is an external user'
# TODO: remove rubocop disable - https://gitlab.com/gitlab-org/gitlab/issues/14960 # TODO: remove rubocop disable - https://gitlab.com/gitlab-org/gitlab/issues/14960
optional :avatar, type: File, desc: 'Avatar image for user' # rubocop:disable Scalability/FileUploads optional :avatar, type: File, desc: 'Avatar image for user' # rubocop:disable Scalability/FileUploads
optional :theme_id, type: Integer, default: 1, desc: 'The GitLab theme for the user'
optional :color_scheme_id, type: Integer, default: 1, desc: 'The color scheme for the file viewer'
optional :private_profile, type: Boolean, desc: 'Flag indicating the user has a private profile' optional :private_profile, type: Boolean, desc: 'Flag indicating the user has a private profile'
all_or_none_of :extern_uid, :provider all_or_none_of :extern_uid, :provider
......
...@@ -66,5 +66,9 @@ module Gitlab ...@@ -66,5 +66,9 @@ module Gitlab
default default
end end
end end
def self.valid_ids
SCHEMES.map(&:id)
end
end end
end end
...@@ -77,6 +77,10 @@ module Gitlab ...@@ -77,6 +77,10 @@ module Gitlab
end end
end end
def self.valid_ids
THEMES.map(&:id)
end
private private
def default_id def default_id
......
...@@ -361,6 +361,12 @@ msgstr "" ...@@ -361,6 +361,12 @@ msgstr ""
msgid "%{percent}%{percentSymbol} complete" msgid "%{percent}%{percentSymbol} complete"
msgstr "" msgstr ""
msgid "%{placeholder} is not a valid color scheme"
msgstr ""
msgid "%{placeholder} is not a valid theme"
msgstr ""
msgid "%{primary} (%{secondary})" msgid "%{primary} (%{secondary})"
msgstr "" msgstr ""
......
...@@ -461,7 +461,7 @@ describe API::Users do ...@@ -461,7 +461,7 @@ describe API::Users do
end end
it "creates user with optional attributes" do it "creates user with optional attributes" do
optional_attributes = { confirm: true } optional_attributes = { confirm: true, theme_id: 2, color_scheme_id: 4 }
attributes = attributes_for(:user).merge(optional_attributes) attributes = attributes_for(:user).merge(optional_attributes)
post api('/users', admin), params: attributes post api('/users', admin), params: attributes
...@@ -576,6 +576,15 @@ describe API::Users do ...@@ -576,6 +576,15 @@ describe API::Users do
expect(response).to have_gitlab_http_status(400) expect(response).to have_gitlab_http_status(400)
end end
it "doesn't create user with invalid optional attributes" do
optional_attributes = { theme_id: 50, color_scheme_id: 50 }
attributes = attributes_for(:user).merge(optional_attributes)
post api('/users', admin), params: attributes
expect(response).to have_gitlab_http_status(400)
end
it 'returns 400 error if user does not validate' do it 'returns 400 error if user does not validate' do
post api('/users', admin), post api('/users', admin),
params: { params: {
...@@ -824,6 +833,34 @@ describe API::Users do ...@@ -824,6 +833,34 @@ describe API::Users do
expect(user.reload.email).not_to eq('invalid email') expect(user.reload.email).not_to eq('invalid email')
end end
it "updates theme id" do
put api("/users/#{user.id}", admin), params: { theme_id: 5 }
expect(response).to have_gitlab_http_status(200)
expect(user.reload.theme_id).to eq(5)
end
it "does not update invalid theme id" do
put api("/users/#{user.id}", admin), params: { theme_id: 50 }
expect(response).to have_gitlab_http_status(400)
expect(user.reload.theme_id).not_to eq(50)
end
it "updates color scheme id" do
put api("/users/#{user.id}", admin), params: { color_scheme_id: 5 }
expect(response).to have_gitlab_http_status(200)
expect(user.reload.color_scheme_id).to eq(5)
end
it "does not update invalid color scheme id" do
put api("/users/#{user.id}", admin), params: { color_scheme_id: 50 }
expect(response).to have_gitlab_http_status(400)
expect(user.reload.color_scheme_id).not_to eq(50)
end
context 'when the current user is not an admin' do context 'when the current user is not an admin' do
it "is not available" do it "is not available" do
expect do expect do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment