Skip to content

Commit

Permalink
Upgrade dependencies (#61)
Browse files Browse the repository at this point in the history
* Shards update

* Let the app compile

* Fix model specs

* Fix controller specs

* Render partial

* CSRF token
  • Loading branch information
veelenga authored Dec 23, 2017
1 parent e235878 commit 16fc35e
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 94 deletions.
1 change: 0 additions & 1 deletion config/application.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Amber::Server.configure do |app|
app.name = "Crystal [ANN] web application."
app.port = AMBER_PORT.to_i
app.host = "0.0.0.0"
app.env = AMBER_ENV.to_s
app.log = ::Logger.new(STDOUT)
app.log.level = ::Logger::INFO
end
Expand Down
8 changes: 5 additions & 3 deletions config/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ Amber::Server.configure do |app|
pipeline :web do
# Plug is the method to use connect a pipe (middleware)
# A plug accepts an instance of HTTP::Handler
plug Amber::Pipe::Logger.new unless app.env == "test"
plug Amber::Pipe::Flash.new
plug Amber::Pipe::Error.new
plug Amber::Pipe::Logger.new
# plug Amber::Pipe::Flash.new
plug Amber::Pipe::Session.new
plug Amber::Pipe::CSRF.new
end

# All static content will run these transformations
pipeline :static do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Static.new("./public")
plug HTTP::CompressHandler.new
end

routes :static do
# Each route is defined as follow
# verb resource : String, controller : Symbol, action : Symbol
get "/*", StaticController, :index
get "/*", Amber::Controller::Static, :index
get "/about", StaticController, :about
end

Expand Down
48 changes: 22 additions & 26 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,51 @@ version: 1.0
shards:
amber:
github: amberframework/amber
version: 0.2.6
version: 0.3.7

autolink:
github: crystal-community/autolink.cr
version: 0.1.3

baked_file_system:
github: schovi/baked_file_system
version: 0.9.4
version: 0.9.5

callback:
github: mosop/callback
version: 0.5.1
version: 0.6.3

cli:
github: mosop/cli
version: 0.5.2
version: 0.6.10

db:
github: crystal-lang/crystal-db
version: 0.4.2
version: 0.4.3

granite_orm:
github: kemalyst/granite-orm
version: 0.7.2
version: 0.7.8

hashids:
github: splattael/hashids.cr
version: 0.2.1

icr:
github: TechMagister/crystal-icr
commit: b28bb938cc9e9e439424217c074a9168d253ba4b

kemal:
github: kemalcr/kemal
version: 0.19.0
version: 0.21.0

kemal-csrf:
github: kemalcr/kemal-csrf
version: 0.2.0
version: 0.3.0

kemal-session:
github: kemalcr/kemal-session
commit: ec08bfe302d6ddbd1b33c5283d56a8b496cdb276
version: 0.8.0

kilt:
github: jeromegn/kilt
version: 0.3.3
version: 0.4.0

micrate:
github: juanedi/micrate
Expand All @@ -62,15 +58,15 @@ shards:

mysql:
github: crystal-lang/crystal-mysql
version: 0.3.2
version: 0.3.3

optarg:
github: mosop/optarg
version: 0.4.4
version: 0.5.8

pg:
github: will/crystal-pg
version: 0.13.3
version: 0.13.4

pool:
github: ysbaddaden/pool
Expand All @@ -82,35 +78,35 @@ shards:

redis:
github: stefanwille/crystal-redis
version: 1.8.0
version: 1.9.0

sentry:
github: TechMagister/sentry
commit: a78129ec1e66aa24c91b93199ea187e4793c8d58
github: samueleaton/sentry
version: 0.1.1

shell-table:
github: jwaldrip/shell-table.cr
version: 0.9.1
version: 0.9.2

sidekiq:
github: mperham/sidekiq.cr
commit: d91d68f8064ea83d252a4002cf492f65516c7a70
commit: 3a15baf85049772db6d67e12821c6993ad9d04ec

slang:
github: jeromegn/slang
commit: b817c89c7e5ae39562710c0d6c7f42cee685e14f
version: 1.7.0

spec2:
github: waterlink/spec2.cr
version: 0.11.0

spinner:
github: elorest/spinner
github: askn/spinner
version: 0.1.1

sqlite3:
github: crystal-lang/crystal-sqlite3
version: 0.8.2
version: 0.8.3

string_inflection:
github: mosop/string_inflection
Expand All @@ -126,5 +122,5 @@ shards:

webmock:
github: manastech/webmock.cr
commit: c673f393a31eac2734c7599897357ef1f3f24011
commit: 7e7e7ff4d00afbb708909e1721e9f9bceddda726

4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license: MIT
dependencies:
granite_orm:
github: kemalyst/granite-orm
version: 0.7.2
version: 0.7.8

pg:
github: will/crystal-pg
Expand All @@ -30,7 +30,7 @@ dependencies:

amber:
github: amberframework/amber
version: 0.2.6
version: 0.3.7

twitter-crystal:
github: sferik/twitter-crystal
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/announcement_controller_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe AnnouncementController do

context "with type param" do
it "can find announcements by type" do
a1 = announcement(user, type: 0).tap &.save
a2 = announcement(user, type: 0).tap &.save
a3 = announcement(user, type: 1).tap &.save
a1 = announcement(user, type: 0_i64).tap &.save
a2 = announcement(user, type: 0_i64).tap &.save
a3 = announcement(user, type: 1_i64).tap &.save

get "/announcements", body: "type=#{Announcement::TYPES[0]}"
expect(response.body.includes? a1.title.to_s).to be_true
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/error_controller_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "./spec_helper"

describe Amber::Controller::Error do
describe "GET index" do
it "returns 404 not found" do
get "/index"
expect(response.status_code).to eq 404
expect(response.body).to eq "404 - Page not found"
end

it "returns 404 not found for any unknown request" do
get "/blah-no-exists"
expect(response.body).to eq "404 - Page not found"
end
end
end
18 changes: 14 additions & 4 deletions spec/controllers/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end
{% for method in %w(get head post put patch delete) %}
def {{method.id}}(path, headers : HTTP::Headers? = nil, body : String? = nil)
request = HTTP::Request.new("{{method.id}}".upcase, path, headers, body)
request.headers["Content-Type"] = Amber::Router::Params::URL_ENCODED_FORM
request.headers["Content-Type"] = Amber::Router::ParamsParser::URL_ENCODED_FORM
Global.response = process_request request
end
{% end %}
Expand All @@ -49,9 +49,19 @@ def process_request(request)
end

def build_main_handler
amber = Amber::Server.settings
amber.handler.prepare_pipelines
amber.handler
handler = Amber::Pipe::Pipeline.new
handler.build :web do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Session.new
plug Amber::Pipe::CSRF.new
end
handler.build :static do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Static.new("./public")
plug HTTP::CompressHandler.new
end
handler.prepare_pipelines
handler
end

def response
Expand Down
13 changes: 0 additions & 13 deletions spec/controllers/static_controller_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,4 @@ describe StaticController do
expect(response.status_code).to eq 200
end
end

describe "GET index" do
it "returns 404 not found" do
get "/index"
expect(response.status_code).to eq 200
expect(response.body).to eq "404 - Page not found"
end

it "returns 404 not found for any unknown request" do
get "/blah-no-exists"
expect(response.body).to eq "404 - Page not found"
end
end
end
20 changes: 10 additions & 10 deletions spec/models/announcement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ describe Announcement do
end

it "validates type" do
expect(announcement(type: -1).valid?).to be_false
expect(announcement(type: -1_i64).valid?).to be_false
end
end

describe "#typename" do
it "returns the properly capitalized type name" do
expect(announcement(type: 0).typename).to eq "Blog post"
expect(announcement(type: 1).typename).to eq "Project update"
expect(announcement(type: 2).typename).to eq "Conference"
expect(announcement(type: 3).typename).to eq "Meetup"
expect(announcement(type: 4).typename).to eq "Podcast"
expect(announcement(type: 5).typename).to eq "Screencast"
expect(announcement(type: 6).typename).to eq "Video"
expect(announcement(type: 7).typename).to eq "Other"
expect(announcement(type: 0_i64).typename).to eq "Blog post"
expect(announcement(type: 1_i64).typename).to eq "Project update"
expect(announcement(type: 2_i64).typename).to eq "Conference"
expect(announcement(type: 3_i64).typename).to eq "Meetup"
expect(announcement(type: 4_i64).typename).to eq "Podcast"
expect(announcement(type: 5_i64).typename).to eq "Screencast"
expect(announcement(type: 6_i64).typename).to eq "Video"
expect(announcement(type: 7_i64).typename).to eq "Other"
end

it "raises error if type is wrong" do
raise_error { announcement(type: -1).typename }
raise_error { announcement(type: -1_i64).typename }
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/models/user_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ describe User do
an = announcement(user: user).tap &.save
an.created_at = Time.new(2016, 2, 15, 10, 20, 30)
an.save
an = announcement(user: user).tap &.save
an = announcement(user: user).tap &.save
announcement(user: user).tap &.save
announcement(user: user).tap &.save
expect(user.last_hour_announcements).to eq 2
end

Expand Down
6 changes: 3 additions & 3 deletions spec/support/factories.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def user(**params)
:uid => (rand() * 10000).to_i.to_s,
:login => "johndoe",
:provider => "github",
} of Symbol => String | Int64
} of Symbol | String => String | JSON::Type

params.each { |k, v| attributes[k] = v }
User.new attributes
Expand All @@ -15,8 +15,8 @@ def announcement(**params)
attributes = {
:title => "title",
:description => "description",
:type => Announcement::TYPES.keys.first,
} of Symbol => String | Int64 | Int32
:type => Announcement::TYPES.keys.first.as(Int64),
} of Symbol | String => String | JSON::Type

params.each { |k, v| attributes[k] = v }
Announcement.new attributes
Expand Down
15 changes: 15 additions & 0 deletions src/controllers/error_controller.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Amber::Controller::Error < Amber::Controller::Base
LAYOUT = "application.slang"

def forbidden
"403 - Forbidden"
end

def not_found
"404 - Page not found"
end

def internal_server_error
"500 - Internal server error"
end
end
4 changes: 0 additions & 4 deletions src/controllers/static_controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ class StaticController < ApplicationController
def about
render("about.slang")
end

def index
"404 - Page not found"
end
end
16 changes: 8 additions & 8 deletions src/models/announcement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ require "autolink"

class Announcement < Granite::ORM::Base
TYPES = {
0 => "blog_post",
1 => "project_update",
2 => "conference",
3 => "meetup",
4 => "podcast",
5 => "screencast",
6 => "video",
7 => "other",
0_i64 => "blog_post",
1_i64 => "project_update",
2_i64 => "conference",
3_i64 => "meetup",
4_i64 => "podcast",
5_i64 => "screencast",
6_i64 => "video",
7_i64 => "other",
}

adapter pg
Expand Down
2 changes: 1 addition & 1 deletion src/models/user.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class User < Granite::ORM::Base
@@adapter.open do |db|
db.scalar(%Q{
SELECT COUNT(*) FROM announcements
WHERE user_id = $1 AND created_at > (NOW() - INTERVAL '1 HOUR')
WHERE user_id = $1 AND created_at > ((NOW() at time zone 'utc') - INTERVAL '1 HOUR')
}, id).as(Int64)
end
end
Expand Down
Loading

0 comments on commit 16fc35e

Please sign in to comment.