Skip to content

Commit 99762e7

Browse files
committed
Fix tests for task comment
1 parent 1e03731 commit 99762e7

File tree

15 files changed

+148
-28
lines changed

15 files changed

+148
-28
lines changed

services/app/apps/codebattle/lib/codebattle/task.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ defmodule Codebattle.Task do
123123
asserts: params.asserts,
124124
asserts_examples: Map.get(params, :asserts_examples, []),
125125
comment: Map.get(params, :comment),
126-
conflict: params.conflict,
127126
creator_id: params[:creator_id],
128127
description_en: params.description_en,
129128
description_ru: params.description_ru,

services/app/apps/codebattle/lib/codebattle/user.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule Codebattle.User do
22
@moduledoc """
33
Represents authenticatable user
44
"""
5+
56
use Ecto.Schema
67

78
import Ecto.Changeset
@@ -63,6 +64,7 @@ defmodule Codebattle.User do
6364
field(:is_bot, :boolean, default: false)
6465
field(:lang, :string, default: "js")
6566
field(:name, :string)
67+
field(:password_hash, :string)
6668
field(:public_id, :binary_id)
6769
field(:rank, :integer, default: 5432)
6870
field(:rating, :integer, default: 1200)
@@ -199,6 +201,38 @@ defmodule Codebattle.User do
199201
|> Repo.update()
200202
end
201203

204+
def authenticate(name, password) do
205+
__MODULE__
206+
|> Repo.get_by(name: name)
207+
|> case do
208+
nil -> nil
209+
user -> verify_password(user, password)
210+
end
211+
end
212+
213+
defp verify_password(_user, nil), do: nil
214+
215+
defp verify_password(user, password) do
216+
if Bcrypt.verify_pass(password, user.password_hash) do
217+
user
218+
else
219+
nil
220+
end
221+
end
222+
223+
def create_password_hash_by_id(user_id, password) do
224+
user_id
225+
|> get!()
226+
|> create_password_hash(password)
227+
end
228+
229+
def create_password_hash(user, password) do
230+
hashed_password = Bcrypt.hash_pwd_salt(password)
231+
232+
from(u in __MODULE__, where: u.id == ^user.id)
233+
|> Repo.update_all(set: [password_hash: hashed_password])
234+
end
235+
202236
def subscription_types, do: @subscription_types
203237

204238
defp assign_clan(changeset, %{:clan => clan}, _user_id) when clan in ["", nil], do: changeset
Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
defmodule CodebattleWeb.SessionController do
22
use CodebattleWeb, :controller
33

4+
alias Codebattle.User
5+
46
def new(conn, _params) do
5-
if Application.get_env(:codebattle, :use_only_token_auth) do
6-
render(conn, "token_only.html")
7-
else
8-
render(conn, "index.html")
7+
cond do
8+
Application.get_env(:codebattle, :use_only_token_auth) ->
9+
render(conn, "token_only.html")
10+
11+
Application.get_env(:codebattle, :use_local_password_auth) || true ->
12+
render(conn, "local_password.html")
13+
14+
:default ->
15+
render(conn, "index.html")
916
end
1017
end
1118

12-
def remind_password(conn, _params) do
13-
render(conn, "index.html")
19+
def create(conn, %{"session" => %{"name" => name, "password" => password}})
20+
when is_binary(name) and is_binary(password) do
21+
case User.authenticate(name, password) do
22+
nil ->
23+
conn
24+
|> put_flash(:danger, gettext("Invalid name or password"))
25+
|> redirect(to: "/session/new")
26+
27+
user ->
28+
conn
29+
|> put_flash(:info, gettext("Welcome to Codebattle!"))
30+
|> put_session(:user_id, user.id)
31+
|> redirect(to: "/")
32+
end
1433
end
1534

35+
def create(conn, _params), do: render(conn, "index.html")
36+
1637
def delete(conn, _params) do
1738
conn
1839
|> put_flash(:info, gettext("You have been logged out!"))
1940
|> configure_session(drop: true)
2041
|> redirect(to: "/")
2142
end
43+
44+
def remind_password(conn, _params) do
45+
render(conn, "index.html")
46+
end
2247
end

services/app/apps/codebattle/lib/codebattle_web/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ defmodule CodebattleWeb.Router do
149149

150150
get("/", RootController, :index)
151151

152-
resources("/session", SessionController, singleton: true, only: [:delete, :new])
152+
resources("/session", SessionController, singleton: true, only: [:delete, :new, :create])
153153
get("/remind_password", SessionController, :remind_password)
154154

155155
resources("/tournaments", TournamentController, only: [:index, :show])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="container d-flex align-items-center justify-content-center vh-100">
2+
<%= form_for @conn, Routes.session_path(@conn, :create), [as: :session], fn f -> %>
3+
<div class="mb-3">
4+
<%= label(f, :name, "Name", class: "form-label") %>
5+
<%= text_input(f, :name, class: "form-control", placeholder: "Enter your name") %>
6+
</div>
7+
<div class="mb-3">
8+
<%= label(f, :password, "Password", class: "form-label") %>
9+
<%= password_input(f, :password, class: "form-control", placeholder: "Enter your password") %>
10+
</div>
11+
<div>
12+
<%= submit("Log in", class: "btn btn-primary") %>
13+
</div>
14+
<% end %>
15+
</div>

services/app/apps/codebattle/mix.exs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,23 @@ defmodule Codebattle.MixProject do
4141
[
4242
{:runner, in_umbrella: true, runtime: false},
4343
{:phoenix_gon, in_umbrella: true},
44+
{:bandit, "~> 1.0"},
45+
{:bcrypt_elixir, "~> 2.0"},
4446
{:cowboy, "~> 2.8"},
4547
{:delta, github: "slab/delta-elixir"},
4648
{:diff_match_patch, github: "vtm9/diff_match_patch", override: true},
4749
{:earmark, "~> 1.4"},
4850
{:ecto_psql_extras, "~> 0.2"},
4951
{:ecto_sql, "~> 3.6"},
5052
{:envy, "~> 1.1.1"},
53+
{:exfake, "~> 1.0.0"},
54+
{:finch, "~> 0.16"},
55+
{:fun_with_flags, "~> 1.11"},
56+
{:fun_with_flags_ui, "~> 1.0"},
5157
{:gettext, "~> 0.18"},
5258
{:html_to_image, github: "vtm9/html_to_image"},
5359
{:jason, "~> 1.2"},
60+
{:nimble_csv, "~> 1.1"},
5461
{:phoenix, "~> 1.7"},
5562
{:phoenix_client, github: "vtm9/phoenix_client"},
5663
{:phoenix_ecto, "~> 4.4"},
@@ -62,21 +69,15 @@ defmodule Codebattle.MixProject do
6269
{:plug, "~> 1.14"},
6370
{:plug_cowboy, "~> 2.7"},
6471
{:postgrex, ">= 0.0.0"},
72+
{:recon, "~> 2.5"},
73+
{:req, "~> 0.5.0"},
6574
{:sentry, "~> 10.0"},
75+
{:statistics, "~> 0.6"},
6676
{:telemetry_metrics, "~> 0.6"},
6777
{:telemetry_poller, "~> 1.0"},
68-
{:req, "~> 0.5.0"},
69-
{:finch, "~> 0.16"},
7078
{:timex, "~> 3.6"},
7179
{:typed_struct, "~> 0.3"},
7280
{:yaml_elixir, "~> 2.4"},
73-
{:exfake, "~> 1.0.0"},
74-
{:fun_with_flags, "~> 1.11"},
75-
{:fun_with_flags_ui, "~> 1.0"},
76-
{:recon, "~> 2.5"},
77-
{:bandit, "~> 1.0"},
78-
{:statistics, "~> 0.6"},
79-
{:nimble_csv, "~> 1.1"},
8081

8182
# dev_and_test
8283
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},

services/app/apps/codebattle/priv/gettext/ru/LC_MESSAGES/default.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ msgstr "%{index}) имя: %{name}, рейтинг: %{rating}"
122122
msgid "You have been logged out!"
123123
msgstr "Вы успешно вышли из системы!"
124124

125+
#: lib/codebattle_web/controllers/session_controller.ex:29
126+
msgid "Welcome to Codebattle!"
127+
msgstr "Добро пожаловать в Codebattle!"
128+
129+
#: lib/codebattle_web/controllers/session_controller.ex:24
130+
msgid "Invalid name or password"
131+
msgstr "Неверное имя пользователя или пароль"
132+
125133
#: lib/codebattle_web/controllers/auth_controller.ex:15
126134
msgid "Failed to authenticate."
127135
msgstr "Ошибка аутентификации."
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Codebattle.Repo.Migrations.AddPasswordHashToUsers do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:users) do
6+
add(:password_hash, :text)
7+
end
8+
end
9+
end

services/app/apps/codebattle/test/codebattle/bot/playbook_store_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ defmodule Codebattle.PlaybookStoreTest do
128128
check_result: %{output: "", result: ""},
129129
editor_lang: "js",
130130
editor_text:
131-
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n\nmodule.exports = solution;",
131+
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n// use stdout to debug\n\nmodule.exports = solution;",
132132
id: ^user2_id,
133133
name: "second",
134134
record_id: 1,
@@ -140,7 +140,7 @@ defmodule Codebattle.PlaybookStoreTest do
140140
check_result: %{output: "", result: ""},
141141
editor_lang: "js",
142142
editor_text:
143-
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n\nmodule.exports = solution;",
143+
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n// use stdout to debug\n\nmodule.exports = solution;",
144144
id: ^user1_id,
145145
name: "first",
146146
record_id: 0,
@@ -150,14 +150,14 @@ defmodule Codebattle.PlaybookStoreTest do
150150
check_result: %{output: "", result: ""},
151151
editor_lang: "js",
152152
editor_text:
153-
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n\nmodule.exports = solution;",
153+
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n// use stdout to debug\n\nmodule.exports = solution;",
154154
id: ^user2_id,
155155
name: "second",
156156
record_id: 1,
157157
type: "init"
158158
},
159159
%{
160-
diff: %{delta: [%{delete: 130}, %{insert: "t"}]},
160+
diff: %{delta: [%{delete: 153}, %{insert: "t"}]},
161161
id: ^user1_id,
162162
record_id: 2,
163163
type: "update_editor_data"

services/app/apps/codebattle/test/codebattle_web/integration/game/rematch_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule CodebattleWeb.Integration.Game.RematchTest do
4242
{:ok, _response, socket2} = subscribe_and_join(socket2, GameChannel, game_topic)
4343

4444
editor_text_init =
45-
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n\nmodule.exports = solution;"
45+
"const _ = require(\"lodash\");\nconst R = require(\"rambda\");\n\nconst solution = (a, b) => {\n return 0;\n};\n// use stdout to debug\n\nmodule.exports = solution;"
4646

4747
game = Game.Context.get_game!(game_id)
4848
assert game.state == "playing"

0 commit comments

Comments
 (0)