add deps for testing

pull/254/head
connor rigby 2017-02-27 09:55:20 -08:00
parent 2c3bd6e5ce
commit 9ce4fb3067
10 changed files with 128 additions and 29 deletions

View File

@ -8,3 +8,8 @@ config :logger, :console,
config :farmbot,
path: "/tmp",
config_file_name: "default_config.json"
config :farmbot, auth_callbacks: []
# frontend <-> bot transports.
config :farmbot, transports: []

View File

@ -85,6 +85,12 @@ defmodule Farmbot.Auth do
end
end
@doc """
Purges the token and creds.
"""
@spec purge_token :: :ok | {:error, atom}
def purge_token, do: GenServer.call(__MODULE__, :purge_token)
@doc """
Gets the token.
Will return a token if one exists, nil if not.
@ -145,9 +151,9 @@ defmodule Farmbot.Auth do
@doc """
Casts credentials to the Auth GenServer
"""
@spec interim(String.t, String.t, String.t) :: no_return
@spec interim(String.t, String.t, String.t) :: :ok
def interim(email, pass, server) do
GenServer.cast(__MODULE__, {:interim, {email,pass,server}})
GenServer.call(__MODULE__, {:interim, {email,pass,server}})
end
@doc """
@ -179,10 +185,15 @@ defmodule Farmbot.Auth do
defp s_a, do: Process.send_after(__MODULE__, :new_token, @six_hours)
# casted creds, store them until something is ready to actually try a log in.
def handle_cast({:interim, {email, pass, server}},_) do
def handle_call({:interim, {email, pass, server}}, _from, _state) do
Logger.info ">> Got some new credentials."
put_server(server)
{:noreply, {email,pass,server}}
{:reply, :ok, {email,pass,server}}
end
def handle_call(:purge_token, _, _state) do
put_server(nil)
{:reply, :ok, nil}
end
def handle_call(:try_log_in, _, {email, pass, server}) do
@ -267,14 +278,6 @@ defmodule Farmbot.Auth do
{:noreply, state}
end
def terminate(:normal, state) do
Logger.info("AUTH DIED: #{inspect state}")
end
def terminate(reason, state) do
Logger.error("AUTH DIED: #{inspect {reason, state}}")
end
defp do_callbacks(token) do
spawn fn() ->
for module <- @modules, do: send(module, {:authorization, token})

View File

@ -23,15 +23,6 @@ defmodule Farmbot.BotState do
GenServer.cast(Hardware, {:set_pos, {x, y, z}})
end
@doc """
Sets the current steps per mm.
"""
@spec set_steps_per_mm(integer, integer, integer) :: boolean
def set_steps_per_mm(x,y,z) do
GenServer.call(Configuration,
{:update_config, "steps_per_mm", %{x: x, y: y, z: z}})
end
@doc """
Sets a pin under the given value
"""

View File

@ -1,5 +1,6 @@
defmodule Module.concat([Farmbot,System,"host"]) do
@moduledoc false
@halt_on_exit Application.get_env(:farmbot, :halt_on_reset, true)
@behaviour Farmbot.System
def reboot, do: :ok
def power_off, do: :ok

View File

@ -103,7 +103,7 @@ defmodule Farmbot.Mixfile do
# http stuff
{:poison, "~> 3.0"},
{:ex_json_schema, "~> 0.5.3"},
{:httpoison, github: "edgurgel/httpoison"},
{:httpoison, github: "edgurgel/httpoison", override: true},
{:rsa, "~> 0.0.1"},
# MQTT stuff
@ -132,6 +132,8 @@ defmodule Farmbot.Mixfile do
{:dialyxir, "~> 0.4", only: [:dev], runtime: false},
{:faker, "~> 0.7", only: :test},
{:excoveralls, "~> 0.6", only: :test},
{:exvcr, "~> 0.8", only: :test},
{:mock, "~> 0.2.0", only: :test},
# Web stuff
{:plug, "~> 1.0"},

View File

@ -1,8 +1,78 @@
defmodule Farmbot.AuthTest do
use ExUnit.Case
doctest Farmbot.Auth
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
alias Farmbot.Auth
import Mock
test "the truth" do
assert 1 + 1 == 2
setup_all do
:ok
end
setup do
:ok = Auth.purge_token
:ok
end
test "returns nil if no token" do
thing = Auth.get_token
assert thing == nil
end
test "logs in" do
good_interim()
{:ok, login_token} = Farmbot.Auth.try_log_in
assert match?(%Farmbot.Token{}, login_token)
end
test "gets the current token" do
good_interim()
{:ok, login_token} = Farmbot.Auth.try_log_in
{:ok, token} = Auth.get_token
assert login_token == token
end
test "gets the current server" do
good_interim()
{:ok, server} = Auth.get_server
assert server == "http://localhost:3000"
end
test "doesnt get a token on bad creds" do
bad_interim()
{:error, reason} = Auth.try_log_in
assert reason == :bad_password
end
test "logs in aggressivly" do
good_interim()
{:ok, login_token} = Farmbot.Auth.try_log_in!
assert match?(%Farmbot.Token{}, login_token)
end
test "logs in with creds, then with a secret" do
good_interim()
{:ok, login_token} = Farmbot.Auth.try_log_in
assert match?(%Farmbot.Token{}, login_token)
Process.sleep(100)
{:ok, new_token} = Farmbot.Auth.try_log_in
assert match?(%Farmbot.Token{}, new_token)
end
test "factory resets the bot on bad log in" do
bad_interim()
with_mock Farmbot.System, [factory_reset: fn -> :ok end] do
Auth.try_log_in!()
assert called Farmbot.System.factory_reset()
end
end
defp good_interim do
:ok = Auth.interim("admin@admin.com", "password123", "http://localhost:3000")
end
defp bad_interim do
:ok = Auth.interim("fail@fail.org", "password", "http://localhost:3000")
end
end

View File

@ -0,0 +1,18 @@
defmodule Farmbot.HTTPTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
alias Farmbot.Auth
alias Farmbot.HTTP
# import Mock
setup_all do
:ok = Auth.interim("admin@admin.com", "password123", "http://localhost:3000")
{:ok, token} = Auth.try_log_in
[token: token]
end
test "makes an api request" do
{:ok, resp} = HTTP.get "/api/corpuses"
assert match?(%HTTPoison.Response{}, resp)
end
end

View File

@ -1 +0,0 @@
ExUnit.start()

View File

@ -24,7 +24,7 @@ defmodule Farmbot.BotStateTest do
assert(is_integer(x) and is_integer(y) and is_integer(z))
end
test("Sets a new position") do
test "Sets a new position" do
Farmbot.BotState.set_pos(45, 123, -666)
[x,y,z] = Farmbot.BotState.get_current_pos
assert(x == 45)
@ -64,7 +64,6 @@ defmodule Farmbot.BotStateTest do
x = Farmbot.BotState.get_config(:steps_per_mm_x)
y = Farmbot.BotState.get_config(:steps_per_mm_y)
z = Farmbot.BotState.get_config(:steps_per_mm_z)
assert([x,y,z] == [123,456,789])
fail = Farmbot.BotState.update_config("self_destruct_count_down", 10_000)
@ -137,4 +136,11 @@ defmodule Farmbot.BotStateTest do
|> Map.get(:hardware)
|> Map.get(part)
end
# defp get_config_part(part) do
# Process.sleep(10)
# Farmbot.BotState.Monitor.get_state
# |> Map.get(:configuration)
# |> Map.get(part)
# end
end

View File

@ -15,9 +15,11 @@
"ex_json_schema": {:hex, :ex_json_schema, "0.5.3", "f2db8eb71ca7a836607d67021d1b9c24f192dd5d1ec6b6b1c89bc934fec3bf5e", [:mix], []},
"ex_syslogger": {:hex, :ex_syslogger, "1.3.3", "914be2c8d759d60d853790815e6cf853e4cea9e24922c01b488c172ba0b7e105", [:mix], [{:poison, ">= 1.5.0", [hex: :poison, optional: true]}, {:syslog, "~> 1.0.2", [hex: :syslog, optional: false]}]},
"ex_webpack": {:hex, :ex_webpack, "0.1.1", "d9deca1f9adfa1fa99ee2630f79756d741f17d4865bc808cd44577304fd053e3", [:mix], []},
"exactor": {:hex, :exactor, "2.2.3", "a6972f43bb6160afeb73e1d8ab45ba604cd0ac8b5244c557093f6e92ce582786", [:mix], []},
"excoveralls": {:hex, :excoveralls, "0.6.2", "0e993d096f1fbb6e70a3daced5c89aac066bda6bce57829622aa2d1e2b338cfb", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"exquisite": {:hex, :exquisite, "0.1.7", "4106503e976f409246731b168cd76eb54262bd04f4facc5cba82c2f53982aaf0", [:mix], []},
"exvcr": {:hex, :exvcr, "0.8.7", "e76f33b10dfefbcf32afa6d6867140566d0d54797e352b47485eed0241dd7edf", [:mix], [{:exactor, "~> 2.2", [hex: :exactor, optional: false]}, {:exjsx, "~> 3.2", [hex: :exjsx, optional: false]}, {:httpoison, "~> 0.8", [hex: :httpoison, optional: true]}, {:httpotion, "~> 3.0", [hex: :httpotion, optional: true]}, {:ibrowse, "~> 4.2.2", [hex: :ibrowse, optional: true]}, {:meck, "~> 0.8.3", [hex: :meck, optional: false]}]},
"faker": {:hex, :faker, "0.7.0", "2c42deeac7be717173c78c77fb3edc749fb5d5e460e33d01fe592ae99acc2f0d", [:mix], []},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
"gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [:mix], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, optional: false]}]},
@ -28,9 +30,11 @@
"ibrowse": {:hex, :ibrowse, "4.2.2", "b32b5bafcc77b7277eff030ed32e1acc3f610c64e9f6aea19822abcadf681b4b", [:rebar3], []},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
"jsx": {:hex, :jsx, "2.8.1", "1453b4eb3615acb3e2cd0a105d27e6761e2ed2e501ac0b390f5bbec497669846", [:mix, :rebar3], []},
"meck": {:hex, :meck, "0.8.4", "59ca1cd971372aa223138efcf9b29475bde299e1953046a0c727184790ab1520", [:make, :rebar], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"mock": {:hex, :mock, "0.2.1", "bfdba786903e77f9c18772dee472d020ceb8ef000783e737725a4c8f54ad28ec", [:mix], [{:meck, "~> 0.8.2", [hex: :meck, optional: false]}]},
"mustache": {:hex, :mustache, "0.0.2", "870fbef411c47d17df06a57b8b3f69e26eb110cba0177c34e273db5d94369d9b", [:mix], []},
"nerves": {:git, "https://github.com/nerves-project/nerves.git", "084e3acecdc0d838671441bd75a9f4b1250bb689", [branch: "target"]},
"nerves_firmware": {:git, "https://github.com/nerves-project/nerves_firmware.git", "b783df3867c82dc0abe6770079077a6f922c00a9", []},