Fix more tests related to time

This commit is contained in:
Ari Timonen 2020-03-25 03:13:38 +02:00
parent 62a49a0072
commit 0b033be5ca
8 changed files with 20 additions and 16 deletions

View file

@ -63,7 +63,7 @@ class ApplicationController < ActionController::Base
def update_user def update_user
if cuser if cuser
Time.zone = cuser.time_zone Time.zone = cuser.time_zone
cuser.update_attribute :lastvisit, DateTime.now if cuser.lastvisit and cuser.lastvisit < 2.minutes.ago cuser.update_attribute :lastvisit, Time.now.utc if cuser&.lastvisit < 2.minutes.ago.utc
if cuser.banned? Ban::TYPE_SITE if cuser.banned? Ban::TYPE_SITE
session[:user] = nil session[:user] = nil

View file

@ -55,8 +55,6 @@ class UsersController < ApplicationController
def create def create
@user = User.new(User.params(params, cuser, "create")) @user = User.new(User.params(params, cuser, "create"))
# FIXME: move to model
@user.lastvisit = Date.today
@user.lastip = request.env['REMOTE_ADDR'] @user.lastip = request.env['REMOTE_ADDR']
raise AccessError unless @user.can_create? cuser raise AccessError unless @user.can_create? cuser
@ -135,7 +133,7 @@ class UsersController < ApplicationController
def save_session user def save_session user
session[:user] = user.id session[:user] = user.id
user.lastip = request.ip user.lastip = request.ip
user.lastvisit = DateTime.now user.lastvisit = Time.now.utc
user.save user.save
end end
end end

View file

@ -301,8 +301,8 @@ class Match < ActiveRecord::Base
end end
def hltv_record(addr, pwd) def hltv_record(addr, pwd)
if (match_time - MATCH_LENGTH * 10) > DateTime.now.utc || if (match_time - MATCH_LENGTH * 10) > Time.now.utc ||
(match_time + MATCH_LENGTH * 10) < DateTime.now.utc (match_time + MATCH_LENGTH * 10) < Time.now.utc
raise Error, I18n.t(:hltv_request_20) raise Error, I18n.t(:hltv_request_20)
end end
if hltv && hltv.recording if hltv && hltv.recording

View file

@ -46,7 +46,7 @@ class User < ActiveRecord::Base
#attr_protected :id, :created_at, :updated_at, :lastvisit, :lastip, :password, :version #attr_protected :id, :created_at, :updated_at, :lastvisit, :lastip, :password, :version
attr_accessor :raw_password attr_accessor :raw_password
#attribute :lastvisit, :string, default: DateTime.now attribute :lastvisit, :datetime, default: Time.now.utc
belongs_to :team belongs_to :team
has_one :profile, :dependent => :destroy has_one :profile, :dependent => :destroy
@ -192,13 +192,13 @@ class User < ActiveRecord::Base
def age def age
return 0 unless birthdate return 0 unless birthdate
a = Date.today.year - birthdate.year a = Time.zone.today.year - birthdate.year
a-= 1 if Date.today < birthdate + a.years a-= 1 if Time.zone.today < birthdate + a.years
a a
end end
def idle def idle
"%d m" % [TimeDifference.between(DateTime.now, lastvisit).in_minutes.floor] "%d m" % [TimeDifference.between(Time.now.utc, lastvisit).in_minutes.floor]
end end
def current_layout def current_layout
@ -214,7 +214,7 @@ class User < ActiveRecord::Base
end end
def banned? type = Ban::TYPE_SITE def banned? type = Ban::TYPE_SITE
Ban.where("expiry > UTC_TIMESTAMP() AND user_id = ? AND ban_type = ?", self.id, type).exists? bans.effective.where(ban_type: type).count > 0
end end
def admin? def admin?

View file

@ -6,7 +6,7 @@ services:
tty: true tty: true
stdin_open: true stdin_open: true
# Debug # Debug
command: /bin/bash # command: /bin/bash
container_name: ensl_dev container_name: ensl_dev
build: build:
context: ./ context: ./
@ -29,6 +29,7 @@ services:
# Debug # Debug
#stdin_open: true #stdin_open: true
command: ["/bin/bash", "-c", "--", "while true; do sleep 100; done;"] command: ["/bin/bash", "-c", "--", "while true; do sleep 100; done;"]
container_name: ensl_test
build: build:
context: ./ context: ./
dockerfile: Dockerfile.dev dockerfile: Dockerfile.dev
@ -56,6 +57,7 @@ services:
redis: redis:
image: 'redis:4.0-alpine' image: 'redis:4.0-alpine'
container_name: ensl_dev_redis
selenium: selenium:
image: selenium/standalone-chrome-debug image: selenium/standalone-chrome-debug

View file

@ -5,7 +5,7 @@ RSpec.describe UsersController, type: :controller do
let!(:params) { FactoryBot.attributes_for(:user) } let!(:params) { FactoryBot.attributes_for(:user) }
let!(:invalid_params) { params.merge(:steamid => (50..150).map { (65 + rand(26)).chr }.join) } let!(:invalid_params) { params.merge(:steamid => (50..150).map { (65 + rand(26)).chr }.join) }
let!(:admin) { create(:user, :admin) } let!(:admin) { create(:user, :admin) }
let!(:user) { create(:user) } let!(:user) { create(:user).reload }
before :all do before :all do
create(:user) create(:user)

View file

@ -1,7 +1,11 @@
FactoryBot.define do FactoryBot.define do
factory :ban, class: Ban do factory :ban, class: Ban do
ban_type { Ban::TYPE_SITE } ban_type { Ban::TYPE_SITE }
expiry { Time.now.utc.to_date + 1 } # NOTE: due to time zone difference this causes tests to fail
# When adding the time, its in previous day and the time is set to 00:00
# read: http://danilenko.org/2012/7/6/rails_timezones/
expiry { Time.now.utc + 1.day }
# Hack because of the awkward way bans are created (requires user_name) # Hack because of the awkward way bans are created (requires user_name)
before(:create) do |ban| before(:create) do |ban|
if ban.user.nil? if ban.user.nil?
@ -25,7 +29,7 @@ FactoryBot.define do
end end
trait :expired do trait :expired do
expiry { Date.yesterday - 1 } expiry { Time.now.utc - 1.day }
end end
end end
end end

View file

@ -42,7 +42,7 @@ describe Topic do
topic = create :topic, first_post: "Foo" topic = create :topic, first_post: "Foo"
topics.push(topic) topics.push(topic)
end end
recent_topics = Topic.recent_topicsbyebug recent_topics = Topic.recent_topics
topics.last(5).each do |topic| topics.last(5).each do |topic|
expect(recent_topics).to include(topic) expect(recent_topics).to include(topic)
end end