fixes user_controller specs, adds user model specs for settings

This commit is contained in:
zmagod 2017-10-05 15:53:05 +02:00
parent a020e23286
commit c20f0c2d8a
7 changed files with 174 additions and 193 deletions

View file

@ -1,9 +1,8 @@
import React, { Component } from "react"; import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import styled from "styled-components"; import styled from "styled-components";
import { FormattedMessage } from "react-intl"; import { FormattedMessage } from "react-intl";
import { getStatisticsInfo } from "../../../../../services/api/users_api";
import MyStatisticsBox from "./MyStatisticsBox"; import MyStatisticsBox from "./MyStatisticsBox";
const Wrapper = styled.div` const Wrapper = styled.div`
@ -16,103 +15,76 @@ class MyStatistics extends Component {
super(props); super(props);
this.state = { this.state = {
statistics: { stats: false,
teamSum: 0, number_of_teams: 0,
projectsSum: 0, number_of_projects: 0,
experimentsSum: 0, number_of_experiments: 0,
protocolsSum: 0 number_of_protocols: 0
}
}; };
this.setData = this.setData.bind(this); this.getStatisticsInfo = this.getStatisticsInfo.bind(this);
} }
componentDidMount() { componentDidMount() {
this.getStatisticsInfo(); this.getStatisticsInfo();
} }
setData({ data }) { getStatisticsInfo() {
const user = data.user; getStatisticsInfo()
.then(response => {
const newData = { this.setState(Object.assign({}, response.statistics, { stats: true }));
statistics: { })
teamsSum: user.statistics.number_of_teams, .catch(error => {
projectsSum: user.statistics.number_of_projects, this.setState({ stats: false });
experimentsSum: user.statistics.number_of_experiments, console.log(error);
protocolsSum: user.statistics.number_of_protocols });
}
};
this.setState(Object.assign({}, this.state, newData));
} }
getStatisticsInfo() { renderStatBoxes() {
// axios if (this.state.stats) {
// .get("/client_api/users/statistics_info") return (
// .then(response => this.setData(response)) <Wrapper>
// .catch(error => console.log(error)); <MyStatisticsBox
typeLength={this.state.number_of_teams}
plural="settings_page.teams"
singular="settings_page.team"
/>
<MyStatisticsBox
typeLength={this.state.number_of_projects}
plural="settings_page.projects"
singular="settings_page.project"
/>
<MyStatisticsBox
typeLength={this.state.number_of_experiments}
plural="settings_page.experiments"
singular="settings_page.experiment"
/>
<MyStatisticsBox
typeLength={this.state.number_of_protocols}
plural="settings_page.protocols"
singular="settings_page.protocol"
/>
</Wrapper>
);
}
return (
<div>
<FormattedMessage id="general.loading" />
</div>
);
} }
render() { render() {
const stats = this.state.statistics;
const statBoxes = () => {
let boxes = (
<div>
<FormattedMessage id="general.loading" />
</div>
);
if (stats) {
boxes = (
<Wrapper>
<MyStatisticsBox
typeLength={stats.teamsSum}
plural="settings_page.teams"
singular="settings_page.team"
/>
<MyStatisticsBox
typeLength={stats.projectsSum}
plural="settings_page.projects"
singular="settings_page.project"
/>
<MyStatisticsBox
typeLength={stats.experimentsSum}
plural="settings_page.experiments"
singular="settings_page.experiment"
/>
<MyStatisticsBox
typeLength={stats.protocolsSum}
plural="settings_page.protocols"
singular="settings_page.protocol"
/>
</Wrapper>
);
}
return boxes;
};
return ( return (
<div> <div>
<h2> <h2>
<FormattedMessage id="settings_page.my_statistics" /> <FormattedMessage id="settings_page.my_statistics" />
</h2> </h2>
{this.renderStatBoxes()}
{statBoxes()}
</div> </div>
); );
} }
} }
MyStatistics.propTypes = { export default MyStatistics;
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);

View file

@ -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 USER_PROFILE_INFO = "/client_api/users/profile_info";
export const UPDATE_USER_PATH = "/client_api/users/update"; export const UPDATE_USER_PATH = "/client_api/users/update";
export const PREFERENCES_INFO_PATH = "/client_api/users/preferences_info" export const PREFERENCES_INFO_PATH = "/client_api/users/preferences_info"
export const STATISTICS_INFO_PATH = "/client_api/users/statistics_info"
// info dropdown_title // info dropdown_title
export const CUSTOMER_SUPPORT_LINK = "http://scinote.net/support"; export const CUSTOMER_SUPPORT_LINK = "http://scinote.net/support";

View file

@ -3,7 +3,8 @@ import {
USER_PROFILE_INFO, USER_PROFILE_INFO,
UPDATE_USER_PATH, UPDATE_USER_PATH,
CURRENT_USER_PATH, CURRENT_USER_PATH,
PREFERENCES_INFO_PATH PREFERENCES_INFO_PATH,
STATISTICS_INFO_PATH
} from "./endpoints"; } from "./endpoints";
export const getUserProfileInfo = () => export const getUserProfileInfo = () =>
@ -25,3 +26,6 @@ export const updateUser = (params, formObj = false) => {
export const getCurrentUser = () => export const getCurrentUser = () =>
axiosInstance.get(CURRENT_USER_PATH).then(({ data }) => data.user); axiosInstance.get(CURRENT_USER_PATH).then(({ data }) => data.user);
export const getStatisticsInfo = () =>
axiosInstance.get(STATISTICS_INFO_PATH).then(({ data }) => data.user);

View file

@ -414,8 +414,9 @@ class User < ApplicationRecord
end end
# json friendly attributes # json friendly attributes
NOTIFICATIONS_TYPES = %w(assignments_notification assignments_email_notification NOTIFICATIONS_TYPES = %w(assignments_notification recent_notification
recent_notification recent_email_notification assignments_email_notification
recent_email_notification
system_message_email_notification) system_message_email_notification)
# declare notifications getters # declare notifications getters
NOTIFICATIONS_TYPES.each do |name| NOTIFICATIONS_TYPES.each do |name|

View file

@ -37,6 +37,7 @@ Rails.application.routes.draw do
put '/update_role', to: 'user_teams#update_role' put '/update_role', to: 'user_teams#update_role'
get '/profile_info', to: 'users#profile_info' get '/profile_info', to: 'users#profile_info'
get '/preferences_info', to: 'users#preferences_info' get '/preferences_info', to: 'users#preferences_info'
get '/statistics_info', to: 'users#statistics_info'
post '/update', to: 'users#update' post '/update', to: 'users#update'
devise_scope :user do devise_scope :user do
put '/invite_users', to: 'invitations#invite_users' put '/invite_users', to: 'invitations#invite_users'

View file

@ -4,6 +4,7 @@ describe ClientApi::Users::UsersController, type: :controller do
login_user login_user
before do before do
# user password is set in user factory defaults to 'asdf1243'
@user = User.first @user = User.first
end end
@ -14,210 +15,203 @@ describe ClientApi::Users::UsersController, type: :controller do
end end
end end
describe 'POST change_password' do describe 'POST update' do
it 'responds successfully' do let(:new_password) { 'secretPassword' }
post :change_password,
params: { user: { 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 format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'changes password' do it 'responds unsuccessfully if no current_password is provided' do
expect(@user.valid_password?('secretPassword')).to eq(false) post :update,
post :change_password, params: { user: { password: new_password,
params: { user: { password: 'secretPassword'} }, password_confirmation: new_password } },
format: :json format: :json
expect(@user.reload.valid_password?('secretPassword')).to eq(true) expect(response).to have_http_status(:unprocessable_entity)
end end
it 'does not change short password' do it 'responds unsuccessfully if no password_confirmation is provided' do
expect(@user.valid_password?('pass')).to eq(false) post :update,
post :change_password, params: { user: { password: new_password,
params: { user: { password: 'pass'} }, current_password: 'asdf1243' } },
format: :json format: :json
expect(@user.reload.valid_password?('pass')).to eq(false) expect(response).to have_http_status(:unprocessable_entity)
end end
end
describe 'POST change_timezone' do it 'responds successfully if time_zone is updated' do
it 'responds successfully' do post :update, params: { user: { time_zone: 'Pacific/Fiji' } },
user = User.first format: :json
expect(user.time_zone).to eq('UTC')
post :change_timezone, params: { timezone: 'Pacific/Fiji' }, format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'changes timezone' do it 'changes timezone' do
user = User.first user = User.first
expect(user.time_zone).to eq('UTC') 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') expect(user.reload.time_zone).to eq('Pacific/Fiji')
end end
end
describe 'POST change_initials' do it 'responds successfully if initials are provided' do
it 'responds successfully' do post :update, params: { user: { initials: 'TD' } }, format: :json
post :change_initials, params: { initials: 'TD' }, format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'responds successfully' do it 'updates user initials' do
user = User.first user = User.first
expect(user.initials).not_to eq('TD') 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') expect(user.reload.initials).to eq('TD')
end end
end
describe 'POST change_system_notification_email' do it 'responds successfully if system_message_email_notification provided' do
it 'responds successfully' do post :update,
post :change_system_notification_email, params: { user: { system_message_email_notificationatus: 'false' } },
params: { status: false },
format: :json format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'changes notification from false => true' do it 'changes system_message_email_notification from false => true' do
user = User.first user = User.first
user.system_message_notification_email = false user.system_message_email_notification = false
user.save user.save
post :change_system_notification_email, post :update,
params: { status: true }, params: { user: { system_message_email_notification: true } },
format: :json 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 end
it 'changes notification from true => false' do it 'changes notification from true => false' do
user = User.first user = User.first
user.system_message_notification_email = true user.recent_email_notification = true
user.save user.save
post :change_system_notification_email, post :update,
params: { status: false }, params: { user: { recent_email_notification: false } },
format: :json format: :json
expect(user.reload.system_message_notification_email).to eq(false) expect(user.reload.recent_email_notification).to eql('false')
end end
end
describe 'POST change_recent_notification_email' do it 'responds successfully if recent_notification provided' do
it 'responds successfully' do post :update, params: { user: { recent_notification: false } },
post :change_recent_notification_email, format: :json
params: { status: false },
format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'changes notification from false => true' do it 'changes recent_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
user = User.first user = User.first
user.recent_notification = false user.recent_notification = false
user.save user.save
post :change_recent_notification, params: { status: true }, format: :json post :update, params: { user: { recent_notification: true } },
expect(user.reload.recent_notification).to eq(true) format: :json
expect(user.reload.recent_notification).to eql('true')
end end
it 'changes notification from true => false' do it 'changes recent_notification from true => false' do
user = User.first user = User.first
user.recent_notification = true user.recent_notification = true
user.save user.save
post :change_recent_notification, params: { status: false }, format: :json post :update, params: { user: { recent_notification: false } },
expect(user.reload.recent_notification).to eq(false) format: :json
expect(user.reload.recent_notification).to eq('false')
end end
end
describe 'POST change_assignements_notification_email' do it 'responds successfully if assignments_email_notification provided' do
it 'responds successfully' do post :update,
post :change_assignements_notification_email, params: { user: { assignments_email_notification: false } },
params: { status: false },
format: :json format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'changes notification from false => true' do it 'changes assignments_email_notification from false => true' do
user = User.first user = User.first
user.assignments_notification_email = false user.assignments_email_notification = false
user.save user.save
post :change_assignements_notification_email, post :update,
params: { status: true }, params: { user: { assignments_email_notification: true } },
format: :json format: :json
expect(user.reload.assignments_notification_email).to eq(true) expect(user.reload.assignments_email_notification).to eq('true')
end end
it 'changes notification from true => false' do it 'changes assignments_email_notification from true => false' do
user = User.first user = User.first
user.assignments_notification_email = true user.assignments_email_notification = true
user.save user.save
post :change_assignements_notification_email, post :update,
params: { status: false }, params: { user: { assignments_email_notification: false } },
format: :json format: :json
expect(user.reload.assignments_notification_email).to eq(false) expect(user.reload.assignments_email_notification).to eq('false')
end end
end
describe 'POST change_assignements_notification' do it 'responds successfully if assignments_notification provided' do
it 'responds successfully' do post :update,
post :change_assignements_notification, params: { user: { assignments_notification: false } },
params: { status: false },
format: :json format: :json
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end end
it 'changes notification from false => true' do it 'changes assignments_notification from false => true' do
user = User.first user = User.first
user.assignments_notification = false user.assignments_notification = false
user.save user.save
post :change_assignements_notification, post :update,
params: { status: true }, params: { user: { assignments_notification: true } },
format: :json format: :json
expect(user.reload.assignments_notification).to eq(true) expect(user.reload.assignments_notification).to eq('true')
end end
it 'changes notification from true => false' do it 'changes assignments_notification from true => false' do
user = User.first user = User.first
user.assignments_notification = true user.assignments_notification = true
user.save user.save
post :change_assignements_notification, post :update,
params: { status: false }, params: { user: { assignments_notification: false } },
format: :json format: :json
expect(user.reload.assignments_notification).to eq(false) expect(user.reload.assignments_notification).to eq('false')
end end
end end
end end

View file

@ -110,7 +110,6 @@ describe User, type: :model do
it { should validate_presence_of :full_name } it { should validate_presence_of :full_name }
it { should validate_presence_of :initials } it { should validate_presence_of :initials }
it { should validate_presence_of :email } it { should validate_presence_of :email }
it { should validate_presence_of :settings }
it do it do
should validate_length_of(:full_name).is_at_most( should validate_length_of(:full_name).is_at_most(
@ -189,4 +188,13 @@ describe User, type: :model do
end end
end 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 end