MySQL fix timestamps for laravel

Execute the following queries to have your ‘created_at’ column the current timestamp upon creation and your ‘updated_at’ the current timestamp upon modification.

// set the default values
DB::statement("ALTER TABLE $table CHANGE created_at created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP")
DB::statement("ALTER TABLE $table CHANGE updated_at updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP");

Posting a form results in 502 error on PHP, nginx and xdebug

This error broke my head the other day.

Whenever I posted a specific form, I got a 502 error.

I only got it to work when I disabled (as in: rebuild Docker image to not contain) xdebug.

When I posted the form, there was this warning: PHP Warning: Unknown: Input variables exceeded 1000.

A quick search learned that I could set the PHP option max_input_vars to 3000. This made the whole thing working again so I could focus on why this big form has more than 1000 input vars….

; file xdebug.ini

;comment all to disable xdebug
;zend_extension="xdebug.so"
;xdebug.mode=debug,coverage
;xdebug.client_host=host.docker.internal
;xdebug.client_port=9003
;xdebug.start_with_request=trigger

; set PHP directive to allow for maximum 3000 input vars
; PHP Input Vars is a PHP security measure designed to limit the number 
; of post variables you can use to execute a function. It defines the 
; number of variables your server can use when running a function.
max_input_vars = 3000

Switching from Sanctum to JWT

I’ve been debugging for days to get my Ionic app working with the Laravel API backend.

The issue: logging in worked fine, but every subsequent API request failed with a 401.

The reason (I guess): the Ionic/capacitor app works with a referrer of capacitor://localhost. I think this messes up the cookie that is set to maintain the session.

So I had to switch over to something without cookies. Having already spend days on the issues, I went to work with JWT as I’ve done so before.

I followed this tutorial to get me a head-start: https://www.positronx.io/laravel-jwt-authentication-tutorial-user-login-signup-api/

Laravel API returns 401 even while logging in succeeds

For me, it turned out the stateful property in config/sanctum.php was not filled correctly.

After setting it to the default as shown below, it started working.

// file config/sanctum.php
...
    'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
        '%s%s',
        'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
        Sanctum::currentApplicationUrlWithPort()
    ))),
...

Did this not fix your problem?

Check this post which might help: https://stackoverflow.com/a/69858100

Generate random values in Postman to use in your tests

When you want your tests to be able to run whenever you want, you should use values which are random.

In Postman, click on the name of Collection and then open the ‘Pre-request Script’ tab.

There, add the following:

// get a random number between a minimum and a maximum
// gives you current datetime with milliseconds like 2022810_171012_174
postman.setGlobalVariable("getCurrentDate",  () => {
  const date=new Date(); 
  return String(date.getFullYear())  
      + String(date.getMonth()+1) 
      + String(date.getDate()) 
      + '_' 
      + String(date.getHours() < 10 ? "0"+date.getHours() : date.getHours()) 
      + String(date.getMinutes() < 10 ? "0"+date.getMinutes() : date.getMinutes()) 
      + String(date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
      + '_' 
      + String(date.getMilliseconds())
})

You can now use this function in your tests. This enables you to make your strings (like emailaddresses) random by adding the current datetime to it.

To use it, open your test, click on the ‘Pre-request Script’ tab and add the following.

var currentDate = eval(pm.globals.get("getCurrentDate"))();
var randomEmail = `postman-${currentDate}@pauledenburg.com`;
pm.environment.set("randomEmail", randomEmail);

You can now use the generated value in the body of your POST-request by referencing it as {{randomEmail}}

Autoprefixer: The color-adjust shorthand is currently deprecated

The full warning is:

Warning
(2728:3) autoprefixer: Replace color-adjust to print-color-adjust. The color-adjust shorthand is currently deprecated.

The easy fix:

rename color-adjust to print-color-adjust

This didn’t work for me as the issue is in the node_modules directory.

Other fix: add the following to your package.json:

  "resolutions": {
    "autoprefixer": "10.4.5"
  },

Then run the following again: yarn install

Check if a variable is set in Selenium IDE and set it when it’s not declared yet

Sometimes you want to use child/parent like tests.

This enables you to treat the ‘child test’ more like a template which you can re-use. But you might want to influence the variable used in this templated test.

To test whether a variable was set and set it when it was not, you’d do the following.

Testing whether a variable was set is done with this javascript string:

"${randomResellerEmail}" == "$" + "{randomResellerEmail}"

In Selenium IDE this looks like the following.

Test for a variable and set it when it did not exist

Store current datetime in variable in Selenium IDE and use it for a random email address

I use variables all the time. And to be able to re-use a test over and over again, I need random email addresses whenever I fill in forms.

For this I define a variable with the current date and time and then a variable which will hold the email address which uses the current date and time.

My random email address will look like: selenium-20220318_122803@pauledenburg.com

Just store the following as 1 string into the ‘Target’ part of your command.

const date=new Date(); return String(date.getFullYear())  + String(date.getMonth()+1) + String(date.getDate()) + '_' + String(date.getHours() < 10 ? "0"+date.getHours() : date.getHours()) + String(date.getMinutes() < 10 ? "0"+date.getMinutes() : date.getMinutes()) + String(date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())

It will look like this in Selenium IDE:

storing the current datetime in a variable in Selenium IDE

Now you can use this to create your email address which is unique every time you run your test:

And use it when you want to fill a form.

Add bearer authentication to your Swagger endpoint

In your .json definition file:

{
  "swagger": "2.0".
  ...
  "securityDefinitions": {
    "bearerAuth": {
      "type": "apiKey",
      "in": "header",
      "name": "Authorization",
    }
  },
  ...
  "paths": {
    "get": {
      "/path": {
        "security": [
          {"bearerAuth": []}
        ],
        ...
      }
    }
  }

official documentation is here: https://swagger.io/docs/specification/authentication/bearer-authentication/