how do I mock a session_id in my integration test - cakephp

I have this code in one of my controllers
$this->request->getSession()->id()
One of my integration tests fails, because this method call is not returning anything. I tried the following in my test setup, but it does not work:
$this->session([
'id' => 'cb3d42b1-4ea0-44c6-8e29-368e948257eb',
]);
Is there a way to make $this->request->getSession()->id() return a value in the integration test setup?

The \Cake\TestSuite\IntegrationTestTrait::session() method accepts values that are being written into the session for the test request, ie session data, it will not configure any other session aspects like the ID.
Normally CakePHP's session class would set a value of cli for the session id when running on the CLI, simply by doing $this->id('cli'), but as of PHP 7.2 that will not work anymore when there's already been output generated, also as of CakePHP 3.5, CakePHP actively prevents setting the ID in case headers have already been sent, ie you probably won't be able to change the ID in your test cases.
So what can you do? Well, you can settle for a fixed value and set it in your test bootstrap (tests/bootstrap.php), which is what the default test bootstrap in the CakePHP 4.x application template now does too, ie simply do:
session_id('my-custom-id');

Related

Symfony 6 can't send email with mailer (without configured database)

Hello guys i just started to building web using Symfony6 - im trying to send email using mailer however it somehow require database to be configured (+ some special table created for messages...).Maybe there is some workaround so it would work without DB.. - thing is in Symfony 5 there was no problem with that.
If you comment the default configuration in config/packages/messenger.yaml
#Symfony\Component\Mailer\Messenger\SendEmailMessage: async
or set it to null your email should be sent immediately.
By default it's configured to work in async mode via messenger, that's why #glukose 's answer works. It sets sync mode and emails are sent immediately that way.
Your messenger is configured to work via Doctrine, that's why it requires DB. Like this:
MESSENGER_TRANSPORT_DSN=doctrine://default
You can use many other options, like Redis or AMQP, async mode is used for a reason after all.
Anyway async mode won't work without workers started with command php bin/console messenger:consume async. That was my problem because I wasn't aware about what was used. No errors, but emails are not actually sent with Symfony mailer!

Get instance ID when running multiCapabilities in Protractor

I'm using multiCapabilities with Protractor. Is there any way to get the instance ID that my tests is running on. I need this because I want each browser sign in with a different account.
What you want to look for is not "instance ID" but sessionID. You would retrieve this by running the following code in your spec:
browser.getSession().then(function(session) {
// do something with sessionID.
});
This will provide the sessionID for the specific spec in a multiCapabilities configuration.
Apparently it is not possible per this GitHub ticket.
What I did was using a buffer JSON file for keeping my configs per capability and use fs to read from it in onPrepare method that runs per capability. I mark each set of params that is used in the file again to avoid using the same set of params again. This way each capability can sign in with a different user name.
It was very manual but worked!

CakePHP - Session in Console?

I write a CLI API for my Application which sends E-Mails. For sending different languages i tried changing the current language in the session (which works fine in a different controller) but in the CLI it prints:
Fatal Error Error: Call to a member function read() on a non-object
Which remains to
$currentLang = $this->Session->read('Config.language');
Any way to use the session?
There is no component or helper for CLI.
If one needs the session (for testing!), one would use CakeSession::read().
Note: session is something web-frontend-based and you never need it in CLI as there is no way to actually handle sessions there. You need to use a different env() based approach there.

How to work with authentication in local Google App Engine tests written in Go?

I'm building a webapp in Go that requires authentication. I'd like to run local tests using appengine/aetest that validate the authentication behavior. However, I do not see any way to create an aetest.Context with a dummy user. Am I missing something?
I had a similar issue with Python sdk. The gist of the solution is to bypass authentication when tests run locally.
You should have access to the [web] app object at the the test setup time - create a user object and save it into the app (or wherever your get_current_user() method will check).
This will let you unit test all application functions except authentication itself. For the later part you can deploy your latest changes as unpublished google app version, then test authentication and if all works - publish the version.
I've discovered some header values that seem to do the trick. appengine/user/user_dev.go has the following:
X-AppEngine-Internal-User-Email
X-AppEngine-Internal-User-Federated-Identity
X-AppEngine-Internal-User-Federated-Provider
X-AppEngine-Internal-User-Id
X-AppEngine-Internal-User-Is-Admin
If I set those headers on the Context's Request when doing in-process tests, things seem to work as expected. If I set the headers on a request that I create separately, things are less successful, since the 'user.Current()' call consults the Context's Request.
These headers might work in a Python environment as well.

Unit Testing the Server Interface for a Silverlight-Facebook Application

I have a Silverlight 4 client running on a Facebook page hosted on Google App Engine. It's using gminifb to communicate with the Facebook API. The Silverlight client uses POST calls to the URIs for each method and passes the session information from Facebook with each call.
The project's growing and it would be super-useful if I could set up a unit-testing system to make a variety of the server calls so that when I make changes I can ensure everything else still works. I've worked with nUnit before and I like what I've read of PEX but I'm not sure how to apply them to this situation.
What're the choices for creating a test system for this? Pros/cons of each?
How do I get started setting something like this up?
Solved. I did it as follows:
Created a special user account to be used for testing on the server that bypassed the authentication. Only did this on the test environment by checking a debug flag in that environment's settings. This avoided creating any security hole in the live site (since the same debug flag will be false there.)
Created a C#.NET solution to test each API call. The host project is a console app (no need for a GUI) with three reusable synchronous methods:
SendFormRequest(WebRequest request, Dictionary<string,string> pairs),
GetJsonFromResponse(HttpWebResponse response),
and ResetAccount().
These three methods allow the host project to make HTTP requests on the server and to read the JSON responses.
Wrapped each server API call inside a method call in the host project.
Created an nUnit test project in the solution. Then simply created tests that call each wrapper method in the host project, using different parameters and changing values on the server.
Created a series of tests to verify correct error handling for invalid parameters and data.
It's working perfectly and has already identified a few minor issues that have been found. The result is immensely useful and will check for breaking changes on new deployments.

Resources