Browse Source

user_pex: add user pex json api with tests

pull/59/head
Arthur POULET 11 months ago
parent
commit
f350c56e9c
Signed by: Sceptique GPG Key ID: C108A668349EE601
  1. 2
      config.ru
  2. 22
      src/app.rb
  3. 2
      src/systems/achievement.rb
  4. 21
      src/systems/api_response.rb
  5. 2
      src/systems/pex.rb
  6. 73
      src/systems/user_pex.rb
  7. 12
      test/base.rb
  8. 6
      test/fixtures/pex_base.yaml
  9. 6
      test/fixtures/user_base.yaml
  10. 4
      test/pex2_system_test.rb
  11. 42
      test/user_pex_test.rb

2
config.ru

@ -1,3 +1,3 @@
require_relative "./src/app"
run LifePex::Systems::BaseSystem
run LifePex::App

22
src/app.rb

@ -59,7 +59,7 @@ class LifePex::Systems::PublicSystem < Sinatra::Base
set :public_folder, "public"
end
class LifePex::Systems::BaseSystem < Sinatra::Base
class LifePex::App < Sinatra::Base
DocMyRoutes.configure do |config|
config.title = "LifePex"
config.description = "LifePex JSON REST API documentation"
@ -70,12 +70,20 @@ class LifePex::Systems::BaseSystem < Sinatra::Base
use Rack::Csrf, skip: ["*:/api*"] if ENV["LIFEPEX_ENV"] != "test"
use LifePex::Systems::AuthSystem
use LifePex::Systems::PublicSystem
use LifePex::Systems::UserSystem
use LifePex::Systems::PexSystem
use LifePex::Systems::Pex2System
use LifePex::Systems::Achievement
LifePex::Systems.constants
.filter { |system| system.to_s =~ /System$/ }
.each { |system|
use LifePex::Systems.const_get(system)
puts "Loaded #{system.to_s.green}"
}
# use LifePex::Systems::AuthSystem
# use LifePex::Systems::PublicSystem
# use LifePex::Systems::UserSystem
# use LifePex::Systems::PexSystem
# use LifePex::Systems::Pex2System
# use LifePex::Systems::AchievementSystem
# use LifePex::Systems::UserPexSystem
include JSON::API
not_found do

2
src/systems/achievement.rb

@ -1,7 +1,7 @@
require_relative "./csrf.rb"
require_relative "./auth.rb"
class LifePex::Systems::Achievement < LifePex::Systems::AuthSystem
class LifePex::Systems::AchievementSystem < LifePex::Systems::AuthSystem
include LifePex::Systems::CrlfHelper
get "/achievements", auth: [] do

21
src/systems/api_response.rb

@ -0,0 +1,21 @@
module LifePex::Systems::ApiResponse
def api_response(any)
content_type 'application/json'
any.to_json
end
def api_response_entity(message = nil, entity_type = nil, entity = nil)
api_response({
"message" => message,
"entity_type" => entity_type,
entity_type => entity,
}.compact)
end
def api_error(status = 500, message = "Internal error", **more)
halt(status, {
message => message,
**more,
})
end
end

2
src/systems/pex.rb

@ -6,7 +6,7 @@ class LifePex::Systems::PexSystem < LifePex::Systems::AuthSystem
include JSON::API
include LifePex::Systems::CrlfHelper
def my_user_pexs cookies, date = nil
def my_user_pexs(cookies, date = nil)
params = {
user_id: user_id_decoded(cookies),
}

73
src/systems/user_pex.rb

@ -1,11 +1,72 @@
require_relative "./pex.rb"
require_relative "./auth.rb"
require_relative "./api_response"
class LifePex::Systems::UserPex < LifePex::Systems::PexSystem
# include JSON::API # included by PexSystem
class LifePex::Systems::UserPexSystem < LifePex::Systems::AuthSystem
include JSON::API
include LifePex::Systems::ApiResponse
# extend DocMyRoutes::Annotatable # included by PexSystem
# register Sinatra::Namespace # included by PexSystem
namespace '/api/user_pex/v2' do
extend DocMyRoutes::Annotatable # included by PexSystem
register Sinatra::Namespace # included by PexSystem
namespace '/api/user-pex/v1' do
namespace '/pexs' do
summary 'Get the amount of user_pex for a given day'
produces 'application/json'
status_codes [200]
get "/:pex_id/amount/by-date/:date", auth: [], provides: 'json' do
pex_id = params["pex_id"]
date = params["date"]
count = LifePex::UserPex.where(
user_id: current_user_id,
pex_id: pex_id,
created_at: date,
).count
api_response({ count: count, entity_type: 'user_pex' })
end
summary 'Create a new user_pex for a given day'
produces 'application/json'
status_codes [200]
post "/:pex_id/validation", auth: [], provides: 'json' do
pex_id = params["pex_id"]
date = json_params["date"] || Date.today.to_s
user_pex = LifePex::UserPex.new(
user_id: current_user_id,
pex_id: pex_id,
created_at: date,
).save
api_response_entity(
"Successfuly added one user_pex",
"user_pex",
user_pex,
)
end
summary 'Remove an existing user_pex for a given day'
produces 'application/json'
status_codes [200]
delete "/:pex_id/validation", auth: [], provides: 'json' do
pex_id = params["pex_id"]
date = json_params["date"] || Date.today.to_s
user_pex = LifePex::UserPex.find(
user_id: current_user_id,
pex_id: pex_id,
created_at: date,
)
if user_pex
user_pex.destroy
api_response({
message: "Successfuly destroyed 1 user_pex",
entity_type: "user_pex",
count: 1,
})
else
api_error(400, "Nothing to destroy")
end
end
end
end
include LifePex::Systems::ApiList

12
test/base.rb

@ -10,17 +10,17 @@ class LifePexTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
LifePex::Systems::BaseSystem
LifePex::App
end
%i(get delete head).each do |verb|
%i(get head).each do |verb|
define_method "api_#{verb}" do |uri, **headers|
self.send(verb, uri, headers.merge({ "CONTENT_TYPE" => "application/json" }))
end
end
%i(post put patch).each do |verb|
define_method "api_#{verb}" do |uri, body, **headers|
%i(post put patch delete).each do |verb|
define_method "api_#{verb}" do |uri, body = {}, **headers|
header "Accept", "application/json"
header "Content-Type", "application/json"
self.send(verb, uri, body.to_json, headers.merge({
@ -29,6 +29,10 @@ class LifePexTest < Test::Unit::TestCase
end
end
def login_as(username = "test", password = "test")
api_post '/api/user/v1/login', { username: username, password: password }
end
# check if every key of `minimal_data` is contained in `full_data`
def assert_hash_include(minimal_data, full_data)
full_data = full_data.stringify_keys

6
test/fixtures/pex_base.yaml

@ -0,0 +1,6 @@
pexs:
- id: 1
user_id: 1
name: 'pex_test_1'
category: 'category'
amount: 1.234

6
test/fixtures/user_base.yaml

@ -1,5 +1,7 @@
users:
- username: "test"
- id: 1
username: "test"
hashed_password: "$2a$12$yWy1fyQBTGYfwRY7H8QGAubHS3nJiMWAaHl8HhXZcTZnuQxRlqIhu" # test
- username: "testbis"
- id: 2
username: "testbis"
hashed_password: "$2a$12$yWy1fyQBTGYfwRY7H8QGAubHS3nJiMWAaHl8HhXZcTZnuQxRlqIhu" # test

4
test/pex2_system_test.rb

@ -127,6 +127,10 @@ class Pex2SystemTest < LifePexTest
login_as
assert last_response.ok?
api_get "/api/pex/v2/pexs"
assert last_response.ok?
assert_equal 0, api_reponse_body.size, "No pex exist yet"
api_post "/api/pex/v2/pexs", { name: 'pex1', amount: 1.23, category: "cat1" }
assert last_response.ok?
pex_id = api_reponse_body.dig "pex", "id"

42
test/user_pex_test.rb

@ -0,0 +1,42 @@
require_relative "./base"
class UserPexSystemTest < LifePexTest
def setup
@fixture ||= FixtureReader.new # should be in startup
@fixture.apply! "user_base"
@fixture.apply! "pex_base"
end
def cleanup
@fixture.cleanup!
end
def test_user_pex_counter
login_as
assert last_response.ok?
api_get "/api/user-pex/v1/pexs/1/amount/by-date/2021-01-01"
assert last_response.ok?
assert_equal 0, api_reponse_body["count"], "No user pex counted yet"
api_post "/api/user-pex/v1/pexs/1/validation", { date: "2021-01-01" }
assert last_response.ok?
assert_equal "2021-01-01", api_reponse_body.dig("user_pex", "created_at")
assert_equal 1, api_reponse_body.dig("user_pex", "pex_id")
assert_equal 1, api_reponse_body.dig("user_pex", "user_id")
api_get "/api/user-pex/v1/pexs/1/amount/by-date/2021-01-01"
assert last_response.ok?
assert_equal 1, api_reponse_body["count"], "One use pex is added"
api_delete "/api/user-pex/v1/pexs/1/validation", { date: "2021-01-01" }
assert last_response.ok?
api_get "/api/user-pex/v1/pexs/1/amount/by-date/2021-01-01"
assert last_response.ok?
assert_equal 0, api_reponse_body["count"], "No user pex remaining"
api_delete "/api/user-pex/v1/pexs/1/validation", { date: "2021-01-01" }
assert_client_error
end
end
Loading…
Cancel
Save