From c20f0c2d8a14ebce3aa8de12ab51788ac684c903 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 5 Oct 2017 15:53:05 +0200 Subject: [PATCH] fixes user_controller specs, adds user model specs for settings --- .../profile/components/MyStatistics.jsx | 130 +++++------ app/javascript/src/services/api/endpoints.js | 1 + app/javascript/src/services/api/users_api.js | 6 +- app/models/user.rb | 5 +- config/routes.rb | 1 + .../client_api/users/users_controller_spec.rb | 214 +++++++++--------- spec/models/user_spec.rb | 10 +- 7 files changed, 174 insertions(+), 193 deletions(-) diff --git a/app/javascript/src/scenes/SettingsPage/scenes/profile/components/MyStatistics.jsx b/app/javascript/src/scenes/SettingsPage/scenes/profile/components/MyStatistics.jsx index 1ee0d9068..881fa8eee 100644 --- a/app/javascript/src/scenes/SettingsPage/scenes/profile/components/MyStatistics.jsx +++ b/app/javascript/src/scenes/SettingsPage/scenes/profile/components/MyStatistics.jsx @@ -1,9 +1,8 @@ import React, { Component } from "react"; -import { connect } from "react-redux"; -import PropTypes from "prop-types"; import styled from "styled-components"; import { FormattedMessage } from "react-intl"; +import { getStatisticsInfo } from "../../../../../services/api/users_api"; import MyStatisticsBox from "./MyStatisticsBox"; const Wrapper = styled.div` @@ -16,103 +15,76 @@ class MyStatistics extends Component { super(props); this.state = { - statistics: { - teamSum: 0, - projectsSum: 0, - experimentsSum: 0, - protocolsSum: 0 - } + stats: false, + number_of_teams: 0, + number_of_projects: 0, + number_of_experiments: 0, + number_of_protocols: 0 }; - this.setData = this.setData.bind(this); + this.getStatisticsInfo = this.getStatisticsInfo.bind(this); } componentDidMount() { this.getStatisticsInfo(); } - setData({ data }) { - const user = data.user; - - const newData = { - statistics: { - teamsSum: user.statistics.number_of_teams, - projectsSum: user.statistics.number_of_projects, - experimentsSum: user.statistics.number_of_experiments, - protocolsSum: user.statistics.number_of_protocols - } - }; - - this.setState(Object.assign({}, this.state, newData)); + getStatisticsInfo() { + getStatisticsInfo() + .then(response => { + this.setState(Object.assign({}, response.statistics, { stats: true })); + }) + .catch(error => { + this.setState({ stats: false }); + console.log(error); + }); } - getStatisticsInfo() { - // axios - // .get("/client_api/users/statistics_info") - // .then(response => this.setData(response)) - // .catch(error => console.log(error)); + renderStatBoxes() { + if (this.state.stats) { + return ( + + + + + + + ); + } + + return ( +
+ +
+ ); } render() { - const stats = this.state.statistics; - - const statBoxes = () => { - let boxes = ( -
- -
- ); - if (stats) { - boxes = ( - - - - - - - ); - } - - return boxes; - }; - return (

- - {statBoxes()} + {this.renderStatBoxes()}
); } } -MyStatistics.propTypes = { - statistics: PropTypes.shape({ - teamsSum: PropTypes.number.isRequired, - projectsSum: PropTypes.number.isRequired, - experimentsSum: PropTypes.number.isRequired, - protocolsSum: PropTypes.number.isRequired - }) -}; - -const mapStateToProps = state => state.current_user; - -export default connect(mapStateToProps, {})(MyStatistics); +export default MyStatistics; diff --git a/app/javascript/src/services/api/endpoints.js b/app/javascript/src/services/api/endpoints.js index 35f04ed47..66eb648c0 100644 --- a/app/javascript/src/services/api/endpoints.js +++ b/app/javascript/src/services/api/endpoints.js @@ -21,6 +21,7 @@ export const RECENT_NOTIFICATIONS_PATH = "/client_api/recent_notifications"; export const USER_PROFILE_INFO = "/client_api/users/profile_info"; export const UPDATE_USER_PATH = "/client_api/users/update"; export const PREFERENCES_INFO_PATH = "/client_api/users/preferences_info" +export const STATISTICS_INFO_PATH = "/client_api/users/statistics_info" // info dropdown_title export const CUSTOMER_SUPPORT_LINK = "http://scinote.net/support"; diff --git a/app/javascript/src/services/api/users_api.js b/app/javascript/src/services/api/users_api.js index 952c21d64..81f9c2016 100644 --- a/app/javascript/src/services/api/users_api.js +++ b/app/javascript/src/services/api/users_api.js @@ -3,7 +3,8 @@ import { USER_PROFILE_INFO, UPDATE_USER_PATH, CURRENT_USER_PATH, - PREFERENCES_INFO_PATH + PREFERENCES_INFO_PATH, + STATISTICS_INFO_PATH } from "./endpoints"; export const getUserProfileInfo = () => @@ -25,3 +26,6 @@ export const updateUser = (params, formObj = false) => { export const getCurrentUser = () => axiosInstance.get(CURRENT_USER_PATH).then(({ data }) => data.user); + +export const getStatisticsInfo = () => + axiosInstance.get(STATISTICS_INFO_PATH).then(({ data }) => data.user); diff --git a/app/models/user.rb b/app/models/user.rb index 611937f5f..f90185283 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -414,8 +414,9 @@ class User < ApplicationRecord end # json friendly attributes - NOTIFICATIONS_TYPES = %w(assignments_notification assignments_email_notification - recent_notification recent_email_notification + NOTIFICATIONS_TYPES = %w(assignments_notification recent_notification + assignments_email_notification + recent_email_notification system_message_email_notification) # declare notifications getters NOTIFICATIONS_TYPES.each do |name| diff --git a/config/routes.rb b/config/routes.rb index c0a420091..f4dee6cb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,6 +37,7 @@ Rails.application.routes.draw do put '/update_role', to: 'user_teams#update_role' get '/profile_info', to: 'users#profile_info' get '/preferences_info', to: 'users#preferences_info' + get '/statistics_info', to: 'users#statistics_info' post '/update', to: 'users#update' devise_scope :user do put '/invite_users', to: 'invitations#invite_users' diff --git a/spec/controllers/client_api/users/users_controller_spec.rb b/spec/controllers/client_api/users/users_controller_spec.rb index c23d17658..fb5a14691 100644 --- a/spec/controllers/client_api/users/users_controller_spec.rb +++ b/spec/controllers/client_api/users/users_controller_spec.rb @@ -4,6 +4,7 @@ describe ClientApi::Users::UsersController, type: :controller do login_user before do + # user password is set in user factory defaults to 'asdf1243' @user = User.first end @@ -14,210 +15,203 @@ describe ClientApi::Users::UsersController, type: :controller do end end - describe 'POST change_password' do - it 'responds successfully' do - post :change_password, - params: { user: { password: 'secretPassword'} }, + describe 'POST update' do + let(:new_password) { 'secretPassword' } + + it 'responds successfully if all password params are set' do + post :update, + params: { user: { password: new_password, + password_confirmation: new_password, + current_password: 'asdf1243' } }, format: :json expect(response).to have_http_status(:ok) end - it 'changes password' do - expect(@user.valid_password?('secretPassword')).to eq(false) - post :change_password, - params: { user: { password: 'secretPassword'} }, + it 'responds unsuccessfully if no current_password is provided' do + post :update, + params: { user: { password: new_password, + password_confirmation: new_password } }, format: :json - expect(@user.reload.valid_password?('secretPassword')).to eq(true) + expect(response).to have_http_status(:unprocessable_entity) end - it 'does not change short password' do - expect(@user.valid_password?('pass')).to eq(false) - post :change_password, - params: { user: { password: 'pass'} }, + it 'responds unsuccessfully if no password_confirmation is provided' do + post :update, + params: { user: { password: new_password, + current_password: 'asdf1243' } }, format: :json - expect(@user.reload.valid_password?('pass')).to eq(false) + expect(response).to have_http_status(:unprocessable_entity) end - end - describe 'POST change_timezone' do - it 'responds successfully' do - user = User.first - expect(user.time_zone).to eq('UTC') - post :change_timezone, params: { timezone: 'Pacific/Fiji' }, format: :json + it 'responds successfully if time_zone is updated' do + post :update, params: { user: { time_zone: 'Pacific/Fiji' } }, + format: :json expect(response).to have_http_status(:ok) end it 'changes timezone' do user = User.first expect(user.time_zone).to eq('UTC') - post :change_timezone, params: { timezone: 'Pacific/Fiji' }, format: :json + post :update, params: { user: { time_zone: 'Pacific/Fiji' } }, + format: :json expect(user.reload.time_zone).to eq('Pacific/Fiji') end - end - describe 'POST change_initials' do - it 'responds successfully' do - post :change_initials, params: { initials: 'TD' }, format: :json + it 'responds successfully if initials are provided' do + post :update, params: { user: { initials: 'TD' } }, format: :json expect(response).to have_http_status(:ok) end - it 'responds successfully' do + it 'updates user initials' do user = User.first expect(user.initials).not_to eq('TD') - post :change_initials, params: { initials: 'TD' }, format: :json + post :update, params: { user: { initials: 'TD' } }, format: :json expect(user.reload.initials).to eq('TD') end - end - describe 'POST change_system_notification_email' do - it 'responds successfully' do - post :change_system_notification_email, - params: { status: false }, + it 'responds successfully if system_message_email_notification provided' do + post :update, + params: { user: { system_message_email_notificationatus: 'false' } }, format: :json expect(response).to have_http_status(:ok) end - it 'changes notification from false => true' do + it 'changes system_message_email_notification from false => true' do user = User.first - user.system_message_notification_email = false + user.system_message_email_notification = false user.save - post :change_system_notification_email, - params: { status: true }, + post :update, + params: { user: { system_message_email_notification: true } }, format: :json - expect(user.reload.system_message_notification_email).to eq(true) + expect(user.reload.system_message_email_notification).to eql('true') + end + + it 'changes system_message_email_notification from true => false' do + user = User.first + user.system_message_email_notification = true + user.save + + post :update, + params: { user: { system_message_email_notification: false } }, + format: :json + expect(user.reload.system_message_email_notification).to eql('false') + end + + it 'responds successfully if recent_email_notification provided' do + post :update, + params: { user: { recent_email_notification: false } }, + format: :json + expect(response).to have_http_status(:ok) + end + + it 'changes recent_email_notification from false => true' do + user = User.first + user.recent_email_notification = false + user.save + + post :update, + params: { user: { recent_email_notification: true } }, + format: :json + expect(user.reload.recent_email_notification).to eql('true') end it 'changes notification from true => false' do user = User.first - user.system_message_notification_email = true + user.recent_email_notification = true user.save - post :change_system_notification_email, - params: { status: false }, + post :update, + params: { user: { recent_email_notification: false } }, format: :json - expect(user.reload.system_message_notification_email).to eq(false) + expect(user.reload.recent_email_notification).to eql('false') end - end - describe 'POST change_recent_notification_email' do - it 'responds successfully' do - post :change_recent_notification_email, - params: { status: false }, - format: :json + it 'responds successfully if recent_notification provided' do + post :update, params: { user: { recent_notification: false } }, + format: :json expect(response).to have_http_status(:ok) end - it 'changes notification from false => true' do - user = User.first - user.recent_notification_email = false - user.save - - post :change_recent_notification_email, - params: { status: true }, - format: :json - expect(user.reload.recent_notification_email).to eq(true) - end - - it 'changes notification from true => false' do - user = User.first - user.recent_notification_email = true - user.save - - post :change_recent_notification_email, - params: { status: false }, - format: :json - expect(user.reload.recent_notification_email).to eq(false) - end - end - - describe 'POST change_recent_notification' do - it 'responds successfully' do - post :change_recent_notification, params: { status: false }, format: :json - expect(response).to have_http_status(:ok) - end - - it 'changes notification from false => true' do + it 'changes recent_notification from false => true' do user = User.first user.recent_notification = false user.save - post :change_recent_notification, params: { status: true }, format: :json - expect(user.reload.recent_notification).to eq(true) + post :update, params: { user: { recent_notification: true } }, + format: :json + expect(user.reload.recent_notification).to eql('true') end - it 'changes notification from true => false' do + it 'changes recent_notification from true => false' do user = User.first user.recent_notification = true user.save - post :change_recent_notification, params: { status: false }, format: :json - expect(user.reload.recent_notification).to eq(false) + post :update, params: { user: { recent_notification: false } }, + format: :json + expect(user.reload.recent_notification).to eq('false') end - end - describe 'POST change_assignements_notification_email' do - it 'responds successfully' do - post :change_assignements_notification_email, - params: { status: false }, + it 'responds successfully if assignments_email_notification provided' do + post :update, + params: { user: { assignments_email_notification: false } }, format: :json expect(response).to have_http_status(:ok) end - it 'changes notification from false => true' do + it 'changes assignments_email_notification from false => true' do user = User.first - user.assignments_notification_email = false + user.assignments_email_notification = false user.save - post :change_assignements_notification_email, - params: { status: true }, + post :update, + params: { user: { assignments_email_notification: true } }, format: :json - expect(user.reload.assignments_notification_email).to eq(true) + expect(user.reload.assignments_email_notification).to eq('true') end - it 'changes notification from true => false' do + it 'changes assignments_email_notification from true => false' do user = User.first - user.assignments_notification_email = true + user.assignments_email_notification = true user.save - post :change_assignements_notification_email, - params: { status: false }, + post :update, + params: { user: { assignments_email_notification: false } }, format: :json - expect(user.reload.assignments_notification_email).to eq(false) + expect(user.reload.assignments_email_notification).to eq('false') end - end - describe 'POST change_assignements_notification' do - it 'responds successfully' do - post :change_assignements_notification, - params: { status: false }, + it 'responds successfully if assignments_notification provided' do + post :update, + params: { user: { assignments_notification: false } }, format: :json expect(response).to have_http_status(:ok) end - it 'changes notification from false => true' do + it 'changes assignments_notification from false => true' do user = User.first user.assignments_notification = false user.save - post :change_assignements_notification, - params: { status: true }, + post :update, + params: { user: { assignments_notification: true } }, format: :json - expect(user.reload.assignments_notification).to eq(true) + expect(user.reload.assignments_notification).to eq('true') end - it 'changes notification from true => false' do + it 'changes assignments_notification from true => false' do user = User.first user.assignments_notification = true user.save - post :change_assignements_notification, - params: { status: false }, + post :update, + params: { user: { assignments_notification: false } }, format: :json - expect(user.reload.assignments_notification).to eq(false) + expect(user.reload.assignments_notification).to eq('false') end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8fc9b145b..6efd9b025 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -110,7 +110,6 @@ describe User, type: :model do it { should validate_presence_of :full_name } it { should validate_presence_of :initials } it { should validate_presence_of :email } - it { should validate_presence_of :settings } it do should validate_length_of(:full_name).is_at_most( @@ -189,4 +188,13 @@ describe User, type: :model do end end end + + describe 'user settings' do + it { is_expected.to respond_to(:time_zone) } + it { is_expected.to respond_to(:assignments_notification) } + it { is_expected.to respond_to(:assignments_email_notification) } + it { is_expected.to respond_to(:recent_notification) } + it { is_expected.to respond_to(:recent_email_notification) } + it { is_expected.to respond_to(:system_message_email_notification) } + end end