]> granicus.if.org Git - ejabberd/commitdiff
Fix tests, they are now running fine locally
authorMickael Remond <mremond@process-one.net>
Fri, 1 Apr 2016 09:13:48 +0000 (11:13 +0200)
committerMickael Remond <mremond@process-one.net>
Fri, 1 Apr 2016 09:13:48 +0000 (11:13 +0200)
src/mod_http_api.erl
test/ejabberd_auth_mock.exs
test/ejabberd_commands_mock_test.exs
test/ejabberd_oauth_mock.exs
test/ejabberd_sm_mock.exs
test/mod_http_api_mock_test.exs

index 60196f6506b1e08fafc533e2904c85faad37a166..73a07205976e5cdc33daa4fc7c6678c5fc238cfa 100644 (file)
@@ -230,16 +230,16 @@ process([Call], #request{method = 'POST', data = Data, ip = IP} = Req) ->
            ?DEBUG("Bad Request: ~p", [_Err]),
            badrequest_response(<<"Invalid JSON input">>);
          _:_Error ->
-        ?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]),
-        badrequest_response()
+            ?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]),
+            badrequest_response()
     end;
 process([Call], #request{method = 'GET', q = Data, ip = IP} = Req) ->
     Version = get_api_version(Req),
     try
         Args = case Data of
-            [{nokey, <<>>}] -> [];
-            _ -> Data
-        end,
+                   [{nokey, <<>>}] -> [];
+                   _ -> Data
+               end,
         log(Call, Args, IP),
         case check_permissions(Req, Call) of
             {allowed, Cmd, Auth} ->
index bbe905142b59a6201d6c0d5588001b9dced2323f..83019c8ee209d0123bfe6c22a855305b811e79e7 100644 (file)
@@ -41,7 +41,7 @@ defmodule EjabberdAuthMock do
                                Agent.get(@agent, fn users -> Map.get(users, {user, domain}, "") end )
                        end)
                mock(:ejabberd_auth, :check_password,
-                       fn (user, authzid, domain, password)  ->
+                       fn (user, _authzid, domain, password)  ->
                                Agent.get(@agent, fn users ->
                                        Map.get(users, {user, domain}) end) == password
                        end)
index b3f10000ee4b6f70acdf766900b05cad61e00fbb..285585034abf0221b7b2dc3df33e2c463a5baaaa 100644 (file)
@@ -18,7 +18,7 @@
 #
 # ----------------------------------------------------------------------
 
-defmodule EjabberdCommandsTest do
+defmodule EjabberdCommandsMockTest do
        use ExUnit.Case, async: false
 
        @author "jsautret@process-one.net"
@@ -35,8 +35,7 @@ defmodule EjabberdCommandsTest do
        @domain "domain"
 
        require Record
-       Record.defrecord :ejabberd_commands, Record.extract(:ejabberd_commands,
-                                                                                                                               from: "ejabberd_commands.hrl")
+       Record.defrecord :ejabberd_commands, Record.extract(:ejabberd_commands, from_lib: "ejabberd/include/ejabberd_commands.hrl")
 
        setup_all do
                try do
@@ -313,7 +312,7 @@ defmodule EjabberdCommandsTest do
 
        test "API command with admin policy" do
                mock_commands_config
-               
+
                # Register a command test(user, domain) -> {user, domain}
                # with policy=admin
                command_name = :test
@@ -354,7 +353,7 @@ defmodule EjabberdCommandsTest do
                                                                                                                                                                 command_name,
                                                                                                                                                                 [@user, @domain])
 
-               
+
                # An admin with bad password cannot call the command
                assert {:error, :account_unprivileged} ==
                        catch_throw :ejabberd_commands.execute_command(:undefined,
index 2c1b8cf9315b54e0036fcc107de052303123d3a3..81cfdc038e69aa6941eaa93687c41704fe1ed7b5 100644 (file)
@@ -1,5 +1,22 @@
-       #  ejabberd_oauth mock
-       ######################
+# ----------------------------------------------------------------------
+#
+# ejabberd, Copyright (C) 2002-2016   ProcessOne
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# ----------------------------------------------------------------------
 
 defmodule EjabberdOauthMock do
 
@@ -17,7 +34,7 @@ defmodule EjabberdOauthMock do
                expire = 1000000 * megasecs + secs + expiration
                :random.seed now
     token = to_string :random.uniform(100000000)
-               
+
                {:ok, _} = :ejabberd_oauth.associate_access_token(token,
                                                                                                                                                                                                                        [{"resource_owner",
                                                                                                                                                                                                                                {:user, user, domain}},
index 0c2fc1635c3715e35dcc42da9e90ec7a13c215ed..53c2c750fe24af13f0be4ebb6f3e567e5ee98d56 100644 (file)
@@ -1,15 +1,30 @@
-       #  ejabberd_sm mock
-       ######################
+# ----------------------------------------------------------------------
+#
+# ejabberd, Copyright (C) 2002-2016   ProcessOne
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# ----------------------------------------------------------------------
 
 defmodule EjabberdSmMock do
   @author "jsautret@process-one.net"
 
        require Record
-       Record.defrecord :session, Record.extract(:session,
-                                                                                                                                                                               from: "ejabberd_sm.hrl")
-       Record.defrecord :jid, Record.extract(:jid,
-                                                                                                                                                               from: "jlib.hrl")
-       
+       Record.defrecord :session, Record.extract(:session, from_lib: "ejabberd/include/ejabberd_sm.hrl")
+       Record.defrecord :jid, Record.extract(:jid, from_lib: "ejabberd/include/jlib.hrl")
+
        @agent __MODULE__
 
        def init do
@@ -20,9 +35,9 @@ defmodule EjabberdSmMock do
     catch
       :exit, _e -> :ok
     end
-               
+
                {:ok, _pid} = Agent.start_link(fn -> [] end, name: @agent)
-               
+
     mock(:ejabberd_sm, :get_user_resources,
                        fn (user, domain)  -> for s <- get_sessions(user, domain), do: s.resource end)
 
@@ -35,9 +50,9 @@ defmodule EjabberdSmMock do
                                :ok
                                (_, _, _) -> :ok
                        end)
-               
+
        end
-       
+
        def connect_resource(user, domain, resource,
                                                                                         opts \\ [priority: 1, conn: :c2s]) do
                Agent.update(@agent, fn sessions ->
@@ -54,13 +69,13 @@ defmodule EjabberdSmMock do
        end
 
        def disconnect_resource(user, domain, resource, timestamp) do
-               Agent.update(@agent, fn sessions ->                                     
+               Agent.update(@agent, fn sessions ->
                for s <- sessions,
                        s.user != user or s.domain != domain or s.resource != resource, do: s
                end)
                ModLastMock.set_last user, domain, "", timestamp
        end
-       
+
        def get_sessions() do
                Agent.get(@agent, fn sessions -> sessions end)
        end
@@ -77,7 +92,7 @@ defmodule EjabberdSmMock do
                        s.user == user, s.domain == domain,     s.resource == resource, do: s
                end)
        end
-       
+
        def to_record(s) do
                session(usr: {s.user, s.domain, s.ressource},
                                                us: {s.user, s.domain},
@@ -91,7 +106,7 @@ defmodule EjabberdSmMock do
        #     Helpers
        ####################################################################
 
-       
+
   # TODO refactor: Move to ejabberd_test_mock
   def mock(module, function, fun) do
     try do
index ae62f28f943b4ace2d2dc7ed1896b9bb46751bbc..78832d7a503d7c83d9d92c119eea55086a5ed5bd 100644 (file)
@@ -18,7 +18,7 @@
 #
 # ----------------------------------------------------------------------
 
-defmodule ModHttpApiTest do
+defmodule ModHttpApiMockTest do
        use ExUnit.Case, async: false
 
        @author "jsautret@process-one.net"
@@ -36,14 +36,17 @@ defmodule ModHttpApiTest do
        @acommand String.to_atom(@command)
        # default API version
        @version 0
-       
+
        require Record
-       Record.defrecord :request, Record.extract(:request,
-                                                                                                                                                                               from: "ejabberd_http.hrl")
+       Record.defrecord :request, Record.extract(:request, from_lib: "ejabberd/include/ejabberd_http.hrl")
 
        setup_all do
                try do
+      :jid.start
+      :mnesia.start
                        :stringprep.start
+      :ejabberd_config.start([@domain], [])
+      :ejabberd_commands.init
                rescue
                        _ -> :ok
                end
@@ -67,13 +70,16 @@ defmodule ModHttpApiTest do
                        fn (@acommand, {@user, @domain, @userpass, false}, @version) ->
                                {[], {:res, :rescode}}
                        end)
+    :meck.expect(:ejabberd_commands, :get_command_policy,
+                       fn (@acommand) -> {:ok, :user} end)
+               :meck.expect(:ejabberd_commands, :get_commands,
+                       fn () -> [@acommand] end)
                :meck.expect(:ejabberd_commands, :execute_command,
                        fn (:undefined, {@user, @domain, @userpass, false}, @acommand, [], @version) ->
                                :ok
                        end)
 
-               #:ejabberd_logger.start
-               #:ejabberd_logger.set 5
+    :ejabberd_config.add_local_option(:commands, [[{:add_commands, [@acommand]}]])
 
                # Correct Basic Auth call
                req = request(method: :GET,
@@ -84,6 +90,9 @@ defmodule ModHttpApiTest do
                                                                        ip: {{127,0,0,1},60000},
                                                                        host: @domain)
                result = :mod_http_api.process([@command], req)
+
+    # history = :meck.history(:ejabberd_commands)
+
                assert 200 == elem(result, 0) # HTTP code
                assert "0" == elem(result, 2) # command result
 
@@ -104,10 +113,8 @@ defmodule ModHttpApiTest do
 
                assert :meck.validate :ejabberd_auth
                assert :meck.validate :ejabberd_commands
-               #assert :ok = :meck.history(:ejabberd_commands)
        end
 
-
        test "HTTP GET simple command call with OAuth" do
                EjabberdAuthMock.create_user @user, @domain, @userpass
 
@@ -116,14 +123,16 @@ defmodule ModHttpApiTest do
                        fn (@acommand, {@user, @domain, {:oauth, _token}, false}, @version) ->
                                        {[], {:res, :rescode}}
                        end)
+    :meck.expect(:ejabberd_commands, :get_command_policy,
+                       fn (@acommand) -> {:ok, :user} end)
+               :meck.expect(:ejabberd_commands, :get_commands,
+                       fn () -> [@acommand] end)
                :meck.expect(:ejabberd_commands, :execute_command,
                        fn (:undefined, {@user, @domain, {:oauth, _token}, false},
                                        @acommand, [], @version) ->
                                        :ok
                        end)
 
-               #:ejabberd_logger.start
-               #:ejabberd_logger.set 5
 
                # Correct OAuth call
                token = EjabberdOauthMock.get_token @user, @domain, @command
@@ -184,5 +193,5 @@ defmodule ModHttpApiTest do
                #assert :ok = :meck.history(:ejabberd_commands)
        end
 
-       
+
 end