scinote-web/test/models/user_test.rb

154 lines
5.5 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = users(:john)
@user2 = users(:steve)
2017-01-25 20:56:43 +08:00
@team = teams(:biosistemika)
2016-02-12 23:52:43 +08:00
end
2016-10-11 14:25:32 +08:00
should validate_presence_of(:full_name)
should validate_length_of(:full_name).is_at_most(Constants::NAME_MAX_LENGTH)
2016-10-11 14:25:32 +08:00
should validate_presence_of(:initials)
should validate_length_of(:initials)
.is_at_most(Constants::USER_INITIALS_MAX_LENGTH)
2016-02-12 23:52:43 +08:00
2017-01-25 20:56:43 +08:00
# Test password attribute
test 'should have non-blank password' do
@user.password = ''
assert @user.invalid?, 'User with blank email is not valid'
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should have password with at least 8 characters' do
@user.password = '1234567'
assert @user.invalid?, 'User with too short password is not valid'
@user.password = '12345678'
assert_not @user.invalid?,
'User with password longer than 7 characters is valid'
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
# Test email attribute
test 'should have non-blank email' do
@user.email = ''
assert @user.invalid?, 'User with blank email is not valid'
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should have unique email' do
2016-02-12 23:52:43 +08:00
@user.email = @user2.email
2017-01-25 20:56:43 +08:00
assert @user.invalid?, 'User with non-unique email in not valid'
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
# Test methods
test 'should get projects for team' do
team_projects = @user2.projects_by_teams(@team.id)
assert_equal 1, team_projects.size, 'Projects are grouped into one team'
assert_equal 4, team_projects[@team].size, 'Team group has many projects'
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should get archived projects for team' do
team_projects = @user2.projects_by_teams(@team.id, nil, true)
assert_equal 1, team_projects.size, 'Projects are grouped into one team'
assert_equal 2, team_projects[@team].size, 'Team group has many projects'
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort projects by create timestamp ascending' do
team_projects = @user2.projects_by_teams(@team.id, 'old')
2016-02-12 23:52:43 +08:00
first_project = projects(:interfaces)
last_project = projects(:z_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort projects by create timestamp descending' do
team_projects = @user2.projects_by_teams(@team.id)
2016-02-12 23:52:43 +08:00
first_project = projects(:z_project)
last_project = projects(:interfaces)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort projects by project name ascending' do
team_projects = @user2.projects_by_teams(@team.id, 'atoz')
2016-02-12 23:52:43 +08:00
first_project = projects(:a_project)
last_project = projects(:z_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort projects by project name descending' do
team_projects = @user2.projects_by_teams(@team.id, 'ztoa')
2016-02-12 23:52:43 +08:00
first_project = projects(:z_project)
last_project = projects(:a_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort archived projects by create timestamp ascending' do
team_projects = @user2.projects_by_teams(@team.id, 'old', true)
2016-02-12 23:52:43 +08:00
first_project = projects(:a_archived_project)
last_project = projects(:z_archived_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort archived projects by create timestamp descending' do
team_projects = @user2.projects_by_teams(@team.id, nil, true)
2016-02-12 23:52:43 +08:00
first_project = projects(:z_archived_project)
last_project = projects(:a_archived_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort archived projects by project name ascending' do
team_projects = @user2.projects_by_teams(@team.id, 'atoz', true)
2016-02-12 23:52:43 +08:00
first_project = projects(:a_archived_project)
last_project = projects(:z_archived_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should sort archived projects by project name descending' do
team_projects = @user2.projects_by_teams(@team.id, 'ztoa', true)
2016-02-12 23:52:43 +08:00
first_project = projects(:z_archived_project)
last_project = projects(:a_archived_project)
2017-01-25 20:56:43 +08:00
assert_equal first_project, team_projects[@team].first
assert_equal last_project, team_projects[@team].last
2016-02-12 23:52:43 +08:00
end
2017-01-25 20:56:43 +08:00
test 'should get last activities' do
2016-02-12 23:52:43 +08:00
last_activities = @user2.last_activities(0)
first_activity = activities(:twelve)
last_activity = activities(:three)
assert_equal 10, last_activities.size
assert_equal first_activity, last_activities.first
assert_equal last_activity, last_activities.last
2016-10-11 14:25:32 +08:00
end
2016-02-12 23:52:43 +08:00
2017-01-25 20:56:43 +08:00
test 'should get specified number of last activities' do
2016-02-12 23:52:43 +08:00
last_activities = @user2.last_activities(0, 4)
first_activity = activities(:twelve)
last_activity = activities(:nine)
assert_equal 4, last_activities.size
assert_equal first_activity, last_activities.first
assert_equal last_activity, last_activities.last
2016-10-11 14:25:32 +08:00
end
2016-02-12 23:52:43 +08:00
2017-01-25 20:56:43 +08:00
test 'should allow to change time zone' do
2016-02-12 23:52:43 +08:00
assert @user.valid?
2017-01-25 20:56:43 +08:00
@user.time_zone = 'Ljubljana'
2016-02-12 23:52:43 +08:00
assert @user.valid?
end
2017-01-25 20:56:43 +08:00
test 'should validate time zone value' do
2016-02-12 23:52:43 +08:00
assert @user.valid?
2017-01-25 20:56:43 +08:00
@user.time_zone = 'Very Strange Place on Earth'
2016-02-12 23:52:43 +08:00
assert_not @user.valid?
end
2017-01-25 20:56:43 +08:00
test 'should check if time zone value is set' do
2016-02-12 23:52:43 +08:00
assert @user.valid?
@user.time_zone = nil
assert_not @user.valid?
end
end