Symfony2 functional test redirect - php

I am working with Symfony2 and the FOSRestBundle and I now try to write a functional test for my rest api. I want to POST a user name and display it, similar to http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html. I.e., my controller returns a View::createRedirect([...],Codes::HTTP_CREATED) instance.
Now, my test looks like:
[...]
$client = static::createClient(array('debug'=>true));
$request = $client->request('POST', '/names',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
$expression);
$response = $client->getResponse();
\print_r($response);
$this->assertEquals($response->getStatusCode(), 201);
$response = $client->followRedirect();
\print_r($response);
When I run it, I get this output:
Symfony\Component\HttpFoundation\Response Object
(
[...]
[location] => Array
(
[0] => /names/90
)
[...]
There was 1 error:
1) MYNAME\MyBundle\Tests\Controller\RestControllerTest::testAddUser
LogicException: The request was not redirected.
Why wasn't it redirected?
Where is my mistake?
Thank you for any help!

If you want to generate a redirect, your backend should generate a 301 (“permanent”) or 302 (“temporary”) HTTP status. A status code of 201 is not really a redirect, even if you set a Location header. I think it depends on the client implementation what happens on a 201 + Location.

Related

Notification callback is not being received

Im using this laravel package "https://github.com/kanazaca/easypay" to create a MB reference using the Easypay API.
I have this method to create the reference:
public function generateReference()
{
$amount = Session::get('total');
$payment_info = [
't_value' => $amount,
'o_obs' => '',
't_key' => 1
];
$easypay = new EasyPay($payment_info);
$reference = $easypay->createReference();
Session::put('entity', $reference['ep_entity']);
Session::put('reference', $reference['ep_reference']);
Session::put('value', $reference['ep_value']);
}
And it works fine with this code I get some reference codes which can be payed using MB or credit-card.
Then, when a payment is made, easypay will call a "Notification URL".
that we should configure on easypay's backoffice under "URL Configurations".
Because when the easypay service receives the payment they will call the URL that we provided. So I defined a url in the easypay´s backoffice and created a route in the project:
Route::get('/easypay/notification-callback', [
'uses' => 'PaymentController#receiveNotifications',
'as' =>'mb.notifications'
]);
In the api backoffice there is a button that simulates a payment, after this button is clicked nothing happens and if I manually access "http://....ngrok.io/easypay/notification-callback" it appears an empty array:
[]
But in the documentation (https://docs.easypay.pt/workflow/payment-notification) says that when Easypay calls this endpoint, it comes with a few parameters: "ep_cin", "ep_user" and "ep_doc" that will be necessary in the process. So it should not appear an empty array.
Do you know what can be the issue? Im a beginner uing API´s so Im not understnading what the issue can be.
PaymentController receiveNotifications() method:
public function receiveNotifications(Request $request)
{
dd($request->all());
//$easypay = new EasyPay($payment_info);
//$xml = $easypay->processPaymentInfo();
//return \Response::make($xml, '200')->header('Content-Type', 'text/xml'); //must return in text/xml for easypay
}
receiveNotifications() method with log:
public function receiveNotifications(Request $request)
{
//dd($request->all());
Log::info('Showing info: ' .var_export($request->all(),true));
$payment_info = [
'ep_cin' => $request->ep_cin,
'ep_user' => $request->ep_user,
'ep_doc' => $request->ep_doc
];
Log::info('Showing info: ' .var_export($payment_info,true));
//dd($payment_info);
$easypay = new EasyPay($payment_info);
$xml = $easypay->processPaymentInfo();
return \Response::make($xml, '200')->header('Content-Type', 'text/xml'); //must return in text/xml for easypay
}
The session is saved in the session file of the user that visits your website that initiates the payment.
The receiveNotifications would call data from the session file that belongs to the payment gateway server if you were doing anything there. The data isn't matched because the two don't know about each other.
Also, you might not have a Session::save() somewhere in your request handling which writes the session data to file.
Store the reference in a database. Create a model for storing this data, so you can then query that model for the correct reference ID to verify/do stuff.
When the request comes back from the payment gateway use the variables ep_cin, ep_user and ep_doc to get the data from the model.
When you manually request that data you are requesting it with a GET request, which doesn't send the above data along.
The request made by the payment provider will get the result of the DD but that's logged nowhere, so nothing happens.
Log your data for requests triggered by remote api's to see what happens.

Active Collab API: How to get projects

I'm trying out the ActiveCollab API for my first time. I had to use StackOveflow to figure out how to get the API token since the docs don't tell me this.
Below is my code:
/* GET INTENT */
$url = 'https://my.activecollab.com/api/v1/external/login';
$fields = array(
'email' => "email#email.com",
'password' => "****"
);
$intent = curl_post_connector($url, $fields);
$intent = $intent->user->intent;
/* GET TOKEN */
$url = 'https://app.activecollab.com/my_app_id/api/v1/issue-token-intent';
$fields = array(
'intent' => $intent,
'client_name' => 'My App Name',
'client_vendor' => 'My Company Name'
);
$token = curl_post_connector($url, $fields);
$token = $token->token;
Everything above works and get's the token properly. What I find really weird is that I have to use API v1 to get this, and the docs on ActiveCollab's site don't mention any URL for API v5. It seems like this is the approach everything is taking here on StackOverflow.
Now with the token, I try to get my list of projects:
/* GET PROJECT */
$url = 'https://app.activecollab.com/my_app_id/api/v1/users';
$headers = array (
"X-Angie-AuthApiToken" => $token
);
$projects = curl_get_connector($url, $headers);
var_dump($projects);
But this does not work. There is no error returned - it instead returns an array of languages for some reason! I don't want to paste the massive json object here, so instead I'll link you to a photo of it: https://www.screencast.com/t/7p5JuFB4Gu
UPDATE:
When attempting to use the SDK, it works up until I try getting the token (which is just as far as I got without the SDK). I'm getting Server Error 500, and when looking at the logs, it says:
/home/working/public_html/ac/index.php(21): ActiveCollab\SDK\Authenticator\Cloud->issueToken(123456789)
#1 {main}
thrown in /home/working/public_html/ac/SDK/Authenticator/Cloud.php on line 115
This is line 115 of Cloud.php:
throw new InvalidArgumentException("Account #{$account_id} not loaded");
I honestly don't think I did anything wrong... there must be something wrong with my account ID.
Just for kicks, I commented out that line, and the error disappears and the page loads fine - except now I have no token...

Plivo Rent a number Api not Working for me

require 'vendor/autoload.php';
use Plivo\RestAPI;
$auth_id = "My AUTH_ID";
$auth_token = "My AUTH_TOKEN";
$p = new RestAPI($auth_id, $auth_token);
$params = array(
'number' => '12512077502' # Phone number to buy
);
$response = $p->get_number($params);
print_r ($response);
It Will Give me Error Message
Array (
[status] => 404
[response] => Array (
[api_id] => 0b6214ee-aec4-11e5-ae4f-22000ac69a0d
[error] => not found
) )
See here https://www.plivo.com/docs/getting-started/phone-number-api/#rent-a-number
You seem to be using the wrong function (get_number) from the python helper library. The correct one is "buy_phone_number" function which uses PhoneNumber API.
Reference - https://github.com/plivo/plivo-python/blob/master/plivo.py#L175
I was using the Python plivo module and had the same problem.
From Plivo Support: "Use the new API: https://www.plivo.com/docs/api/number/phonenumber/#buy-number "
What I found that the the plivo module uses the wrong URL when renting a phone number. My work around is to make the call without the helper library. The following is Python code but it might help give you an idea what to do.
import requests
params = {
'number' : phone_number # Phone number to buy
}
host = 'https://api.plivo.com/v1/Account/%s/PhoneNumber/%s/' % \
(account_sid, phone_number)
r = requests.post(host, timeout=5, json=params, auth=(account_sid, auth_token))
assert r.status_code == 201, 'r.status_code=%s' % `r.status_code`
Update: The above might not be necessary after all. I just got an update from Plivo support. The new method name is buy_phone_number() instead of get_number(). That solved the problem for me. I assume the same is true for the PHP library.

Codeception API TestGuy sends empty post on SubmitForm

First of all, sorry for my english!
I'm writing functional tests with codeception, i'm using the ZF2 module and so far all the stuff have worked great. The problem comes when i try to submit forms, the test seems to send parameters correctly, but the $_POST is always empty.
My ExempleCept.php
$I = new TestGuy($scenario);
$I->wantTo('perform actions and see result');
$I->amOnPage('/user/login');
$I->submitForm('#login', ['email' => 'teste123#teste.com.br', 'password' => '789456']);
$I->seeCurrentUrlEquals('/user/login');
vendor/bin/codeception run --debug output
* I submit form "#login",{"email":"teste123#teste.com.br","password":"789456"}
array(0) {
}
=> [Response] 200
=> [Page] http://localhost/user/login
* I see current url equals "/user/login"
=> [Uri] /user/login
=> [Method] post
=> [Parameters] {"email":"teste123#teste.com.br","password":"789456"}
=> [Response] 200
=> [Page] http://localhost/user/login
UserController::loginAction()
public function loginAction() {
$form = new LoginForm ( 'login' );
$request = $this->getRequest ();
if ($request->isPost ()) {
var_dump($_POST);
...
Note that the var_dump line on my action, is called right after " * I submit form", so the POST is coming!
So, who can help me?
Thanks!
Edited
I've tried the same test with WebGuy API (acceptance), and it worked. But, the tests must be executed by Jenkins, and WebGuy needs an http server to run, then i realy need the TestGuy tests working

shopify Unprocessable Entity

while running this code with the app instalation,i am getting the Unprocessable Entity status code 422 error.
Here is the code
$sc = new ShopifyClient($_SESSION['shop'], $_SESSION['token'], $api_key, $secret);
$charge = array
(
"webhooks"=> array
(
"topic"=>"orders/create",
"address"=>"http://www.abc123no.com/nomi/s.php?key=123456789",
"format"=>"json"
)
);
try
{
$webhooks = $sc->call('POST','/admin/webhooks.json',$charge);
}
catch (ShopifyApiException $e)
{
var_dump($e->getResponseHeaders());
}
Error code 422 is for validation errors. The body of the response will describe the error to help you debug your application.
e.g. The response might be: {"errors":{"address":["for this topic has already been taken"]}}
Your error appears to be from using the wrong format for the request. The create endpoint doesn't take an array of webhooks, and you should use the singular "webhook" for the top-level key.
The Webhook API documentation has the correct format for the request body.
Change webhooks to webhook , it might create bad request error
"**webhooks**"=> array
(
"topic"=>"orders/create",
"address"=>"http://www.abc123no.com/nomi/s.php?key=123456789",
"format"=>"json"
)

Categories