From 81d6d7e6fe2069a41eb45e66442fb32ceffd0766 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 9 Mar 2015 13:10:51 -0700 Subject: [PATCH] register api #1 --- applications/api.moon | 35 +++++++++++++++++++++++---------- models/api_keys.moon | 14 +++++++++++++ spec/applications/api_spec.moon | 23 +++++++++++++++++----- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/applications/api.moon b/applications/api.moon index e9f24961..5751de56 100644 --- a/applications/api.moon +++ b/applications/api.moon @@ -3,7 +3,7 @@ lapis = require "lapis" import assert_valid from require "lapis.validate" import capture_errors_json, assert_error from require "lapis.application" - +import trim_filter from require "lapis.util" import ApiKeys, Users from require "models" api_request = (fn) -> @@ -32,26 +32,41 @@ format_streak = do class StreakApi extends lapis.Application "/api/1/login": capture_errors_json => + trim_filter @params assert_valid @params, { - { "source", one_of: {"ios"} } + { "source", one_of: ApiKeys.sources } { "username", exists: true } { "password", exists: true } } user = assert_error Users\login @params.username, @params.password + key = ApiKeys\find_or_create user.id, @params.source + + json: { :key } - key = unpack ApiKeys\select [[ - where user_id = ? and source = ? - ]], user.id, ApiKeys.sources\for_db @params.source - unless key - key = ApiKeys\create { - user_id: user.id - source: @params.source - } + "/api/1/register": capture_errors_json => + trim_filter @params + assert_valid @params, { + { "source", one_of: ApiKeys.sources } + { "username", exists: true, min_length: 2, max_length: 25 } + { "password", exists: true, min_length: 2 } + { "password_repeat", equals: @params.password } + { "email", exists: true, min_length: 3 } + } + + assert_error @params.email\match(".@."), "invalid email address" + user = assert_error Users\create { + username: @params.username + email: @params.email + password: @params.password + } + + key = ApiKeys\find_or_create user.id, @params.source json: { :key } + -- Streaks user is in "/api/1/my-streaks": api_request => import Users, Streaks from require "models" diff --git a/models/api_keys.moon b/models/api_keys.moon index 6d203753..39decf85 100644 --- a/models/api_keys.moon +++ b/models/api_keys.moon @@ -21,5 +21,19 @@ class ApiKeys extends Model opts.source = @sources\for_db opts.source Model.create @, opts + + @find_or_create: (user_id, source) => + source = @sources\for_db source + + key = unpack @select [[ + where user_id = ? and source = ? + ]], user_id, source + + unless key + key = @create user_id: user_id, :source + + key + + url_key: => @key diff --git a/spec/applications/api_spec.moon b/spec/applications/api_spec.moon index a29bd830..5b5bf5ac 100644 --- a/spec/applications/api_spec.moon +++ b/spec/applications/api_spec.moon @@ -50,6 +50,23 @@ describe "api", -> assert.same key, res.key + + it "should register user", -> + status, res = request "/api/1/register", { + post: { + source: "ios" + username: "leafo" + password: "leafo" + password_repeat: "leafo" + email: "leafo@example.com" + } + expect: "json" + } + + assert.truthy res.key + assert.same 1, #Users\select! + + describe "with key", -> local api_key, current_user @@ -68,11 +85,7 @@ describe "api", -> assert.same 200, status assert.same { hosted: {} - joined: { - upcoming: {} - active: {} - completed: {} - } + joined: {} }, res