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 { 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 (
<Wrapper>
<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() {
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 (
<div>
<h2>
<FormattedMessage id="settings_page.my_statistics" />
</h2>
{statBoxes()}
{this.renderStatBoxes()}
</div>
);
}
}
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;

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 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";

View file

@ -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);

View file

@ -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|

View file

@ -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'

View file

@ -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

View file

@ -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