Skip to content

Commit

Permalink
testss
Browse files Browse the repository at this point in the history
  • Loading branch information
bitboxer committed Mar 3, 2019
1 parent fca3854 commit 1c65b57
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
9 changes: 3 additions & 6 deletions lib/sign_dict/worker/refresh_sign_writings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ defmodule SignDict.Worker.RefreshSignWritings do

def perform(
entry_id,
fetch_service \\ SignDict.Services.FetchDelegesDataForEntry,
sleep_ms \\ @process_sleep_time
) do
IO.puts("trying things")

Bugsnex.handle_errors %{entry_id: entry_id} do
# Rate limit the workers, sadly i didn't find a better way :(
Process.sleep(sleep_ms)

IO.puts("refreshing it!")

entry = Repo.get(Entry, entry_id) |> IO.inspect()
SignDict.Services.FetchDelegesDataForEntry.fetch_for(entry)
entry = Repo.get(Entry, entry_id)
fetch_service.fetch_for(entry)
end
end
end
25 changes: 18 additions & 7 deletions lib/sign_dict_web/controllers/contact_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@ defmodule SignDictWeb.ContactController do
@recaptcha Application.get_env(:sign_dict, :recaptcha)[:library]

def new(conn, _params) do
IO.inspect Application.get_env(:sign_dict, :recaptcha)
email = user_email(conn.assigns)
render conn, "new.html", layout: {SignDictWeb.LayoutView, "app.html"},
email: email, content: ""

render(conn, "new.html",
layout: {SignDictWeb.LayoutView, "app.html"},
email: email,
content: ""
)
end

def create(conn = %{assigns: %{current_user: current_user}}, params) when not is_nil(current_user) do
def create(conn = %{assigns: %{current_user: current_user}}, params)
when not is_nil(current_user) do
%{"contact" => %{"email" => email, "content" => content}} = params
send_email(conn, email, content)
end

def create(conn, params) do
%{"contact" => %{"email" => email, "content" => content}} = params

case @recaptcha.verify(params["g-recaptcha-response"]) do
{:ok, _response} ->
send_email(conn, email, content)
{:error, _errors} -> show_error(conn, email, content)

{:error, _errors} ->
show_error(conn, email, content)
end
end

defp send_email(conn, email, content) do
mail = Email.contact_form(email, content)
Mailer.deliver_later(mail)

conn
|> put_flash(:info, gettext("The email was sent."))
|> redirect(to: "/")
Expand All @@ -37,8 +45,11 @@ defmodule SignDictWeb.ContactController do
defp show_error(conn, email, content) do
conn
|> put_flash(:error, gettext("The recaptcha was wrong, email not sent."))
|> render("new.html", layout: {SignDictWeb.LayoutView, "app.html"},
email: email, content: content)
|> render("new.html",
layout: {SignDictWeb.LayoutView, "app.html"},
email: email,
content: content
)
end

defp user_email(%{assigns: %{current_user: %{email: email}}}) when not is_nil(email) do
Expand Down
10 changes: 6 additions & 4 deletions lib/sign_dict_web/controllers/entry_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ defmodule SignDictWeb.EntryController do
end

defp refresh_sign_writings(%{entry: entry} = params) do
if entry.deleges_updated_at == nil ||
Timex.before?(entry.deleges_updated_at, Timex.shift(Timex.now(), days: -3)) do
queue = Application.get_env(:sign_dict, :queue)[:library]
queue.enqueue(Exq, "sign_writings", SignDict.Worker.RefreshSignWritings, [entry.id])
if entry do
if entry.deleges_updated_at == nil ||
Timex.before?(entry.deleges_updated_at, Timex.shift(Timex.now(), days: -3)) do
queue = Application.get_env(:sign_dict, :queue)[:library]
queue.enqueue(Exq, "sign_writings", SignDict.Worker.RefreshSignWritings, [entry.id])
end
end

params
Expand Down
4 changes: 0 additions & 4 deletions test/sign_dict/services/fetch_deleges_data_for_entry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ defmodule SignDict.Services.FetchDelegesDataForEntryTest do
# TODO: test result
end

test "it will not update anything if last update younger than three days" do
# todo: implement
end

test "does not update image if image is not modified" do
# todo: implement
end
Expand Down
22 changes: 22 additions & 0 deletions test/sign_dict/worker/refresh_sign_writings_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule SignDict.Worker.RefreshSignWritingsTest do
use SignDict.ModelCase

import SignDict.Factory

alias SignDict.Worker.RefreshSignWritings

defmodule FetchDelegesDataForEntryMock do
def fetch_for(entry) do
send(self(), {:for_entry, entry.id})
:done
end
end

describe "perform/2" do
test "it fetches the images" do
entry_id = insert(:entry).id
RefreshSignWritings.perform(entry_id, FetchDelegesDataForEntryMock)
assert_received {:for_entry, ^entry_id}
end
end
end
31 changes: 30 additions & 1 deletion test/sign_dict_web/controllers/entry_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule SignDict.EntryControllerTest do
alias SignDict.Entry
alias SignDict.Vote

# TODO sign writings jobs enqueued
describe "show/2 entry attributes" do
setup do
entry = insert(:entry)
Expand Down Expand Up @@ -99,6 +98,36 @@ defmodule SignDict.EntryControllerTest do
assert html_response(conn, 200) =~ entry.type
assert html_response(conn, 200) =~ "User 2"
end

test "enqueus sign_writing refresh if never run ", %{conn: conn, entry: entry} do
get(conn, entry_path(conn, :show, entry))
entry_id = entry.id

assert_received {:mock_exq, "sign_writings", SignDict.Worker.RefreshSignWritings,
[^entry_id]}
end

test "enqueus sign_writing refresh if never run with video in the url ", %{
conn: conn,
entry: entry,
video_2: video
} do
get(conn, entry_video_path(conn, :show, entry, video))
entry_id = entry.id

assert_received {:mock_exq, "sign_writings", SignDict.Worker.RefreshSignWritings,
[^entry_id]}
end

test "does not enque if younger than three days", %{conn: conn} do
entry = insert(:entry, deleges_updated_at: Timex.shift(Timex.now(), days: -1))

get(conn, entry_path(conn, :show, entry))
entry_id = entry.id

refute_received {:mock_exq, "sign_writings", SignDict.Worker.RefreshSignWritings,
[^entry_id]}
end
end

describe "new/2" do
Expand Down

0 comments on commit 1c65b57

Please sign in to comment.