<commit_before>require 'spec_helper'

describe 'passbolt_api service' do

  before(:all) do
    @mysql = Docker::Container.create(
      'Env' => [
        'MYSQL_ROOT_PASSWORD=<PASSWORD>',
        'MYSQL_DATABASE=passbolt',
        'MYSQL_USER=passbolt',
        'MYSQL_PASSWORD=<PASSWORD>'
      ],
      "Healthcheck" => {
        "Test": [
          "CMD-SHELL",
          "mysqladmin ping --silent"
        ]
      },
      'Image' => 'mariadb')
    @mysql.start

    while @mysql.json['State']['Health']['Status'] != 'healthy'
      sleep 1
    end

    @image     = Docker::Image.build_from_dir(ROOT_DOCKERFILES)
    @container = Docker::Container.create(
      'Env' => [
        "DATASOURCES_DEFAULT_HOST=#{@mysql.json['NetworkSettings']['IPAddress']}",
        'DATASOURCES_DEFAULT_PASSWORD=<PASSWORD>',
        'DATASOURCES_DEFAULT_USERNAME=passbolt',
        'DATASOURCES_DEFAULT_DATABASE=passbolt',
        'PASSBOLT_SSL_FORCE=true'
      ],
      'Image' => @image.id)
    @container.start
    @container.logs(stdout: true)

    set :docker_container, @container.id
    sleep 17
  end

  after(:all) do
    @mysql.kill
    @container.kill
  end

  let(:healthcheck)       { 'curl -sk -o /dev/null -w "%{http_code}" -H "Host: passbolt.local" https://localhost/healthcheck/status.json' }
  let(:serverkey)         { 'curl -sk -o /dev/null -w "%{http_code}" -H "Host: passbolt.local" https://localhost/config/gpg/serverkey.asc' }
  let(:serverkey_private) { 'curl -sk -o /dev/null -w "%{http_code}" -H "Host: passbolt.local" https://localhost/config/gpg/serverkey_private.asc' }
  let(:tmp)               { 'curl -sk -o /dev/null -w "%{http_code}" -H "Host: passbolt.local" https://localhost/tmp/cache/database/empty' }
  let(:logs)              { 'curl -sk -o /dev/null -w "%{http_code}" -H "Host: passbolt.local" https://localhost/logs/error.log' }
  let(:conf_app)          { 'curl -sk -o /dev/null -w "%{http_code}" -H "Host: passbolt.local" https://localhost/conf/app.php' }

  describe 'php service' do
    it 'is running supervised' do
      expect(service('php-fpm')).to be_running.under('supervisor')
    end

    it 'has its port open' do
      expect(@container.json['Config']['ExposedPorts']).to have_key('9000/tcp')
    end
  end

  describe 'email cron' do
    it 'is running supervised' do
      expect(service('cron')).to be_running.under('supervisor')
    end
  end

  describe 'web service' do
    it 'is running supervised' do
      expect(service('nginx')).to be_running.under('supervisor')
    end

    it 'is listening on port 80' do
      expect(@container.json['Config']['ExposedPorts']).to have_key('80/tcp')
    end

    it 'is listening on port 443' do
      expect(@container.json['Config']['ExposedPorts']).to have_key('443/tcp')
    end
  end

  describe 'passbolt status' do
    it 'returns 200' do
      expect(command(healthcheck).stdout).to eq '200'
    end
  end

  describe 'passbolt serverkey unaccessible' do
    it 'returns 404' do
      expect(command(serverkey).stdout).to eq '404'
    end
  end

  describe 'passbolt serverkey private unaccessible' do
    it 'returns 404' do
      expect(command(serverkey_private).stdout).to eq '404'
    end
  end

  describe 'passbolt tmp folder is unaccessible' do
    it 'returns 404' do
      expect(command(tmp).stdout).to eq '404'
    end
  end

  describe 'passbolt conf files can not be retrieved' do
    it 'returns 404' do
      expect(command(conf_app).stdout).to eq '404'
    end
  end

  describe 'passbolt error log folder is unaccessible' do
    it 'returns 404' do
      expect(command(logs).stdout).to eq '404'
    end
  end

end
<commit_msg>DRY tests<commit_after>require 'spec_helper'

describe 'passbolt_api service' do

  before(:all) do
    @mysql_image = Docker::Image.create('fromImage' => 'mariadb:latest')
    @mysql = Docker::Container.create(
      'Env' => [
        'MYSQL_ROOT_PASSWORD=<PASSWORD>',
        'MYSQL_DATABASE=passbolt',
        'MYSQL_USER=passbolt',
        'MYSQL_PASSWORD=<PASSWORD>'
      ],
      "Healthcheck" => {
        "Test": [
          "CMD-SHELL",
          "mysqladmin ping --silent"
        ]
      },
      'Image' => @mysql_image.id)
    @mysql.start

    while @mysql.json['State']['Health']['Status'] != 'healthy'
      sleep 1
    end

    @image     = Docker::Image.build_from_dir(ROOT_DOCKERFILES)
    @container = Docker::Container.create(
      'Env' => [
        "DATASOURCES_DEFAULT_HOST=#{@mysql.json['NetworkSettings']['IPAddress']}",
        'DATASOURCES_DEFAULT_PASSWORD=<PASSWORD>',
        'DATASOURCES_DEFAULT_USERNAME=passbolt',
        'DATASOURCES_DEFAULT_DATABASE=passbolt',
        'PASSBOLT_SSL_FORCE=true'
      ],
      'Image' => @image.id)
    @container.start
    @container.logs(stdout: true)

    set :docker_container, @container.id
    sleep 17
  end

  after(:all) do
    @mysql.kill
    @container.kill
  end

  let(:passbolt_host)     { @container.json['NetworkSettings']['IPAddress'] }
  let(:uri)               { "/healthcheck/status.json" }
  let(:curl)              { "curl -sk -o /dev/null -w '%{http_code}' -H 'Host: passbolt.local' https://#{passbolt_host}/#{uri}" }
  let(:conf_app)          { "curl -sk -o /dev/null -w '%{http_code}' -H 'Host: passbolt.local' https://#{passbolt_host}/conf/app.php" }

  describe 'php service' do
    it 'is running supervised' do
      expect(service('php-fpm')).to be_running.under('supervisor')
    end

    it 'has its port open' do
      expect(@container.json['Config']['ExposedPorts']).to have_key('9000/tcp')
    end
  end

  describe 'email cron' do
    it 'is running supervised' do
      expect(service('cron')).to be_running.under('supervisor')
    end
  end

  describe 'web service' do
    it 'is running supervised' do
      expect(service('nginx')).to be_running.under('supervisor')
    end

    it 'is listening on port 80' do
      expect(@container.json['Config']['ExposedPorts']).to have_key('80/tcp')
    end

    it 'is listening on port 443' do
      expect(@container.json['Config']['ExposedPorts']).to have_key('443/tcp')
    end
  end

  describe 'passbolt status' do
    it 'returns 200' do
      expect(command(curl).stdout).to eq '200'
    end
  end

  describe 'passbolt serverkey unaccessible' do
    let(:uri) { '/config/gpg/serverkey.asc' }
    it "returns 404" do
      expect(command(curl).stdout).to eq '404'
    end
  end

  describe 'passbolt serverkey private unaccessible' do
    let(:uri) { '/config/gpg/serverkey_private.asc' }
    it 'returns 404' do
      expect(command(curl).stdout).to eq '404'
    end
  end

  describe 'passbolt conf unaccessible' do
    let(:uri) { '/config/app.php' }
    it 'returns 404' do
      expect(command(curl).stdout).to eq '404'
    end
  end
  describe 'passbolt tmp folder is unaccessible' do
    let(:uri) { '/tmp/cache/database/empty' }
    it 'returns 404' do
      expect(command(curl).stdout).to eq '404'
    end
  end

  describe 'passbolt conf files can not be retrieved' do
    let(:uri) { '/config/gpg/serverkey_private.asc' }
    it 'returns 404' do
      expect(command(curl).stdout).to eq '404'
    end
  end

end

{'fraction_non_alphanumeric': 0.14369830365904948, 'fraction_numerical': 0.014860507500350483, 'mean_word_length': 2.8792822185970635, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 20, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4422414', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2356, 'n_words': 539}