aboutsummaryrefslogtreecommitdiff
path: root/src/guff/api/test.cr
blob: 91a729cbcc99d5809cd9f7b2a76866c9e8d8580d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require "json"

module Guff::API::TestAPI
  private def do_test_version(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    {version: Guff::VERSION}.to_json
  end

  private def do_test_get_posts(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    [{foo: "bar"}, {foo: "asdf"}].to_json
  end

  private def do_test_get_users(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    # get user_id
    user_id = 0_i64
    with_session(context) do |session|
      user_id = (session["user_id"]? || "0").to_i64
      false
    end

    # get users
    users = @models.user.get_users

    # return result
    { "users": users, "user_id": user_id }
  end

  private def do_test_set_user(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    with_session(context) do |session|
      session["user_id"] = args["user_id"]
      true
    end

    { ok: true }
  end

  private def do_test_error(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    raise "some random error"
  end

  private def do_test_add_user(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    user_id = @models.user.add_user(args["name"].not_nil!,
      active:   args["active"]? ? args["active"] == "t" : nil,
      role:     args["role"]?,
      email:    args["email"]?,
      password: args["password"]?,
    )
    { "ok": true, "user_id": user_id }
  end

  private def do_test_edit_user(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    @models.user.edit_user(args["user_id"].to_i64,
      name:     args["name"]?,
      active:   args["active"]? ? args["active"] == "t" : nil,
      role:     args["role"]?,
      email:    args["email"]?,
      password: args["password"]?,
    )

    { "ok": true }
  end

  private def do_test_add_login(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    @models.user.add_login(args["user_id"].to_i,
      email:    args["name"],
      password: args["password"],
    )

    { "ok": true }
  end

  private def do_test_remove_login(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    @models.user.remove_login(args["user_id"].to_i)
    { "ok": true }
  end

  private def do_test_login(
    context : HTTP::Server::Context,
    args    : Hash(String, String)
  )
    user_id = @models.user.login(args["email"], args["password"]).to_i
    { "ok": true, "user_id": user_id }
  end
end