From 0b033be5cac253777b2a29a2a83e09600f5a9376 Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Wed, 25 Mar 2020 03:13:38 +0200 Subject: [PATCH] Fix more tests related to time --- app/controllers/application_controller.rb | 2 +- app/controllers/users_controller.rb | 4 +--- app/models/match.rb | 4 ++-- app/models/user.rb | 10 +++++----- docker-compose.dev.yml | 4 +++- spec/controllers/users_controller_spec.rb | 2 +- spec/factories/ban.rb | 8 ++++++-- spec/models/topic_spec.rb | 2 +- 8 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 95a9261..48a189a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -63,7 +63,7 @@ class ApplicationController < ActionController::Base def update_user if cuser 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 session[:user] = nil diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0b2a367..462209a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -55,8 +55,6 @@ class UsersController < ApplicationController def create @user = User.new(User.params(params, cuser, "create")) - # FIXME: move to model - @user.lastvisit = Date.today @user.lastip = request.env['REMOTE_ADDR'] raise AccessError unless @user.can_create? cuser @@ -135,7 +133,7 @@ class UsersController < ApplicationController def save_session user session[:user] = user.id user.lastip = request.ip - user.lastvisit = DateTime.now + user.lastvisit = Time.now.utc user.save end end diff --git a/app/models/match.rb b/app/models/match.rb index ebb6a6d..4a9f1d5 100755 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -301,8 +301,8 @@ class Match < ActiveRecord::Base end def hltv_record(addr, pwd) - if (match_time - MATCH_LENGTH * 10) > DateTime.now.utc || - (match_time + MATCH_LENGTH * 10) < DateTime.now.utc + if (match_time - MATCH_LENGTH * 10) > Time.now.utc || + (match_time + MATCH_LENGTH * 10) < Time.now.utc raise Error, I18n.t(:hltv_request_20) end if hltv && hltv.recording diff --git a/app/models/user.rb b/app/models/user.rb index eb1b778..757d549 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,7 +46,7 @@ class User < ActiveRecord::Base #attr_protected :id, :created_at, :updated_at, :lastvisit, :lastip, :password, :version attr_accessor :raw_password - #attribute :lastvisit, :string, default: DateTime.now + attribute :lastvisit, :datetime, default: Time.now.utc belongs_to :team has_one :profile, :dependent => :destroy @@ -192,13 +192,13 @@ class User < ActiveRecord::Base def age return 0 unless birthdate - a = Date.today.year - birthdate.year - a-= 1 if Date.today < birthdate + a.years + a = Time.zone.today.year - birthdate.year + a-= 1 if Time.zone.today < birthdate + a.years a end def idle - "%d m" % [TimeDifference.between(DateTime.now, lastvisit).in_minutes.floor] + "%d m" % [TimeDifference.between(Time.now.utc, lastvisit).in_minutes.floor] end def current_layout @@ -214,7 +214,7 @@ class User < ActiveRecord::Base end 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 def admin? diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index c5cd782..46ad474 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -6,7 +6,7 @@ services: tty: true stdin_open: true # Debug - command: /bin/bash + # command: /bin/bash container_name: ensl_dev build: context: ./ @@ -29,6 +29,7 @@ services: # Debug #stdin_open: true command: ["/bin/bash", "-c", "--", "while true; do sleep 100; done;"] + container_name: ensl_test build: context: ./ dockerfile: Dockerfile.dev @@ -56,6 +57,7 @@ services: redis: image: 'redis:4.0-alpine' + container_name: ensl_dev_redis selenium: image: selenium/standalone-chrome-debug diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 719723e..0b599e7 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -5,7 +5,7 @@ RSpec.describe UsersController, type: :controller do let!(:params) { FactoryBot.attributes_for(:user) } let!(:invalid_params) { params.merge(:steamid => (50..150).map { (65 + rand(26)).chr }.join) } let!(:admin) { create(:user, :admin) } - let!(:user) { create(:user) } + let!(:user) { create(:user).reload } before :all do create(:user) diff --git a/spec/factories/ban.rb b/spec/factories/ban.rb index 74c5bbb..bd63389 100755 --- a/spec/factories/ban.rb +++ b/spec/factories/ban.rb @@ -1,7 +1,11 @@ FactoryBot.define do factory :ban, class: Ban do 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) before(:create) do |ban| if ban.user.nil? @@ -25,7 +29,7 @@ FactoryBot.define do end trait :expired do - expiry { Date.yesterday - 1 } + expiry { Time.now.utc - 1.day } end end end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index c09959f..2c01565 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -42,7 +42,7 @@ describe Topic do topic = create :topic, first_post: "Foo" topics.push(topic) end - recent_topics = Topic.recent_topicsbyebug + recent_topics = Topic.recent_topics topics.last(5).each do |topic| expect(recent_topics).to include(topic) end