From 8aeb5bd57107a8b60963d530a1851199347002cd Mon Sep 17 00:00:00 2001
From: Oleg Hasjanov <oleg.phenomenon@gmail.com>
Date: Mon, 18 Dec 2023 11:40:39 +0200
Subject: [PATCH] updated billing issues

---
 Dockerfile                                    | 104 +++---------------
 Gemfile                                       |   2 +-
 Gemfile.lock                                  |  11 --
 .../billing_profiles_controller.rb            |   5 +
 config/locales/billing_profiles.en.yml        |   1 +
 config/locales/billing_profiles.et.yml        |   1 +
 test/application_system_test_case.rb          |   6 +
 test/system/auctions_test.rb                  |  25 ++++-
 test/system/billing_profiles_test.rb          |  64 +++++++++--
 9 files changed, 107 insertions(+), 112 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index fb28fe3de..282e11ae0 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,99 +1,21 @@
-# FROM internetee/ruby:3.0-buster
+FROM internetee/ruby:3.2.2-refactor as base
 
-# RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install wkhtmltopdf -y > /dev/null
-
-# RUN npm install -g yarn@latest
-# WORKDIR /opt/webapps/app
-# COPY Rakefile Gemfile Gemfile.lock ./
-# RUN gem install bundler && bundle install --jobs 20 --retry 5
-# COPY package.json yarn.lock ./
-# RUN yarn install --check-files
-
-
-FROM internetee/ruby:3.2.2
-
-SHELL ["/bin/bash", "-o", "pipefail", "-c"]
-RUN apt-get update > /dev/null && apt-get install -y --no-install-recommends > /dev/null \
-    build-essential=* \
-    imagemagick=* \
-    curl \
-    wget \
-    gnupg2 \
-    git \
-    apt-utils \
-    && apt-get dist-upgrade -yf\
-    && apt-get clean \
-    && rm -rf /var/lib/apt/lists/*
-
-RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales
-RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
-RUN sed -i -e 's/# et_EE.UTF-8 UTF-8/et_EE.UTF-8 UTF-8/' /etc/locale.gen && \
-    dpkg-reconfigure --frontend=noninteractive locales && \
-    update-locale LANG=et_EE.UTF-8
-
-ENV LANG et_EE.UTF-8
-RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc -s | apt-key add -
-RUN sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
-RUN apt-get update > /dev/null && apt-get install -y --no-install-recommends > /dev/null \
-    postgresql-client-13=* \
-    libpq-dev \
-    && apt-get clean \
-    && rm -rf /var/lib/apt/lists/*
-
-# add repository for Node.js in the LTS version
-RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
-
-RUN apt-get install -y --no-install-recommends > /dev/null \
-  nodejs=* \
-  qt5-default=* \
-  libqt5webkit5-dev=* \
-  gstreamer1.0-plugins-base=* \
-  libappindicator3-1=* \
-  gstreamer1.0-tools=* \
-  qtdeclarative5-dev=* \
-  fonts-liberation=* \
-  gstreamer1.0-x=* \
-  libasound2=* \
-  libnspr4=* \
-  libnss3=* \
-  libxss1=* \
-  libxtst6=* \
-  xdg-utils=* \
-  qtdeclarative5-dev=* \
-  fonts-liberation=* \
-  gstreamer1.0-x=* \
-  wkhtmltopdf \
-  libxslt1-dev \
-  libxml2-dev \
-  python-dev \
-  && apt-get clean \
-  && rm -rf /var/lib/apt/lists/*
+RUN npm install -g yarn@latest
+COPY --link Gemfile Gemfile.lock ./
+RUN gem install bundler && bundle install --jobs 20 --retry 5
+COPY --link package.json yarn.lock ./
+RUN yarn install --frozen-lockfile
 
-# RUN curl https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip -o /chromedriver_linux64.zip
-# RUN apt-get update > /dev/null \
-#     && apt-get install -yf --no-install-recommends > /dev/null unzip=* \
-#     && apt-get clean \
-#     && rm -rf /var/lib/apt/lists/*
-# RUN unzip chromedriver_linux64.zip -d /usr/local/bin
-# RUN rm /chromedriver_linux64.zip
+FROM base
 
-# RUN npm install --global yarn
-RUN npm install -g yarn@latest
+RUN useradd rails
+RUN mkdir -p /home/rails && chown rails:rails /home/rails
 
-RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /chrome.deb
-RUN dpkg -i /chrome.deb || apt-get update > /dev/null \
-    && apt-get install -yf --no-install-recommends > /dev/null && apt-get clean \
-    && rm -rf /var/lib/apt/lists/*
-RUN dpkg -i /chrome.deb
-RUN rm /chrome.deb
-RUN sed -i 's/SECLEVEL=2/SECLEVEL=1/' /etc/ssl/openssl.cnf
+USER rails:rails
 
-RUN mkdir -p /opt/webapps/app/tmp/pids
-WORKDIR /opt/webapps/app
+RUN mkdir -p /opt/webapps/app/tmp/pids 
 
-COPY Rakefile Gemfile Gemfile.lock ./
-RUN gem install bundler && bundle install --jobs 20 --retry 5
-COPY package.json yarn.lock ./
-RUN yarn install --check-files
+COPY --from=base --chown=rails:rails /usr/local/bundle /usr/local/bundle
+COPY --from=base --chown=rails:rails /opt/webapps/app /opt/webapps/app
 
 EXPOSE 3000
diff --git a/Gemfile b/Gemfile
index f4b4df5aa..a84f98c64 100644
--- a/Gemfile
+++ b/Gemfile
@@ -69,7 +69,7 @@ group :test do
   gem 'capybara', '~> 3.39.2'
   gem 'cuprite'
   # gem 'selenium-webdriver', '4.10.0'
-  gem 'webdrivers'
+  # gem 'webdrivers'
   gem 'simplecov', '~> 0.10', '< 0.18', require: false
   gem 'spy'
   gem 'webmock'
diff --git a/Gemfile.lock b/Gemfile.lock
index eeb11d968..22fcdfaa0 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -386,14 +386,9 @@ GEM
       faraday-multipart (>= 1)
     ruby-progressbar (1.13.0)
     ruby2_keywords (0.0.5)
-    rubyzip (2.3.2)
     scenic (1.7.0)
       activerecord (>= 4.0.0)
       railties (>= 4.0.0)
-    selenium-webdriver (4.12.0)
-      rexml (~> 3.2, >= 3.2.5)
-      rubyzip (>= 1.2.2, < 3.0)
-      websocket (~> 1.0)
     simplecov (0.17.1)
       docile (~> 1.1)
       json (>= 1.8, < 3)
@@ -445,10 +440,6 @@ GEM
       activemodel (>= 6.0.0)
       bindex (>= 0.4.0)
       railties (>= 6.0.0)
-    webdrivers (5.2.0)
-      nokogiri (~> 1.6)
-      rubyzip (>= 1.3.0)
-      selenium-webdriver (~> 4.0)
     webfinger (1.2.0)
       activesupport
       httpclient (>= 2.4)
@@ -460,7 +451,6 @@ GEM
       hkdf (~> 0.2)
       jwt (~> 2.0)
     webrick (1.8.1)
-    websocket (1.2.10)
     websocket-driver (0.7.5)
       websocket-extensions (>= 0.1.0)
     websocket-extensions (0.1.5)
@@ -528,7 +518,6 @@ DEPENDENCIES
   valvat
   view_component
   web-console (>= 3.3.0)
-  webdrivers
   webmock
   webpush
 
diff --git a/app/controllers/billing_profiles_controller.rb b/app/controllers/billing_profiles_controller.rb
index b005307cb..99773e6f0 100644
--- a/app/controllers/billing_profiles_controller.rb
+++ b/app/controllers/billing_profiles_controller.rb
@@ -5,6 +5,11 @@ class BillingProfilesController < ApplicationController
   before_action :authorize_billing_profile_for_user, except: %i[new index create]
   before_action :store_location, only: :new
 
+  rescue_from ActiveRecord::RecordNotUnique do |_exception|
+    flash[:alert] = t('billing_profiles.vat_code_already_exists')
+    render turbo_stream: turbo_stream.replace('flash', partial: 'common/flash', locals: { flash: })
+  end
+
   # GET /billing_profiles
   def index
     @billing_profiles = BillingProfile.accessible_by(current_ability)
diff --git a/config/locales/billing_profiles.en.yml b/config/locales/billing_profiles.en.yml
index a6cdb0d80..abff44dae 100644
--- a/config/locales/billing_profiles.en.yml
+++ b/config/locales/billing_profiles.en.yml
@@ -18,6 +18,7 @@ en:
     updated: "Billing profile successfully updated!"
     deleted: "Billing profile successfully deleted!"
     vat_validation_error: "VAT code is not registered or not valid according to VIES"
+    vat_code_already_exists: "VAT code is already in use"
 
     edit:
       title: "Edit billing profile"
diff --git a/config/locales/billing_profiles.et.yml b/config/locales/billing_profiles.et.yml
index dfc7f7670..363fe8a08 100644
--- a/config/locales/billing_profiles.et.yml
+++ b/config/locales/billing_profiles.et.yml
@@ -18,6 +18,7 @@ et:
     updated: "Arveldusprofiil edukalt uuendatud!"
     deleted: "Arveldusprofiil edukalt kustutatud!"
     vat_validation_error: "Käibemaksukood ei ole registreeritud või ei ole VIESi kohaselt kehtiv."
+    vat_code_already_exists: "Käibemaksukood on juba kasutusel."
 
     edit:
       title: "Muuda arve aadressi"
diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb
index 66c9b0bf1..2965ab826 100644
--- a/test/application_system_test_case.rb
+++ b/test/application_system_test_case.rb
@@ -56,6 +56,12 @@ def using_session(name, &block)
   
   Capybara.server = :puma, { Silent: true }
 
+  def take_screenshot
+    return super unless Capybara.last_used_session
+
+    Capybara.using_session(Capybara.last_used_session) { super }
+  end
+
   private
 
   def extract_primary_link_from_last_mail
diff --git a/test/system/auctions_test.rb b/test/system/auctions_test.rb
index ece6828c2..15c2277f2 100644
--- a/test/system/auctions_test.rb
+++ b/test/system/auctions_test.rb
@@ -118,6 +118,7 @@ def test_filter_auction_list
   # AUTOBIDER ========================================
 
   def test_autobider_available_only_for_english_type_auctions
+    Autobider.destroy_all
     sign_in @user
 
     visit('/')
@@ -153,11 +154,31 @@ def test_autobider_not_available_for_blind_auctions
     refute(page.has_content?(:visible, "#{I18n.t('english_offers.form.autobider')}: #{I18n.t('english_offers.form.nope')}" ))
   end
 
-  def test_autobidder_could_not_be_enable_if_price_is_less_than_minimum_required
+  def test_user_can_set_autobider_and_unset_autobider
+    Autobider.destroy_all
+
+    sign_in @user
+
+    visit('/')
+
+    within("tr[data-platform='english']", match: :first) do
+      assert(page.has_content?(:visible, '0.0 €'))
 
+      click_link_or_button(I18n.t('auctions.bid'))
+    end
+
+    assert_selector '.c-modal', visible: true
+
+    assert(page.has_content?(:visible, "#{I18n.t('english_offers.form.autobider')}: #{I18n.t('english_offers.form.nope')}" ))
+    
+    find('#autobider_price').set('100')
+    # find('.o-checkbox__slider').click
+    find('#checkbox').set(true)
+
+    # TODO: Turbo stream not work in test mode
+    # assert(page.has_content?(:visible, "#{I18n.t('english_offers.form.autobider')}: #{I18n.t('english_offers.form.yep')}" ))
   end
 
-  def test_user_can_set_autobider_and_unset_autobider
 
   # def setup
   #   super
diff --git a/test/system/billing_profiles_test.rb b/test/system/billing_profiles_test.rb
index d0538ff96..c94f8c2dd 100644
--- a/test/system/billing_profiles_test.rb
+++ b/test/system/billing_profiles_test.rb
@@ -1,14 +1,64 @@
 require 'application_system_test_case'
 
 class BillingProfilesTest < ApplicationSystemTestCase
-  # def setup
-  #   super
 
-  #   @user = users(:participant)
-  #   @billing_profile = billing_profiles(:private_person)
-  #   @company_billing_profile = billing_profiles(:company)
-  #   sign_in(@user)
-  # end
+  def setup
+    super
+
+    @user = users(:participant)
+    @billing_profile = billing_profiles(:private_person)
+    @company_billing_profile = billing_profiles(:company)
+    sign_in(@user)
+  end
+
+  def test_user_can_change_his_billing_profile
+    Offer.destroy_all
+    @user.reload
+    @billing_profile.reload
+
+    visit billing_profiles_path
+
+    user_name_field = find('.o-card__title.c-account__title', match: :first)
+    assert_equal @user.display_name, user_name_field.value
+    assert user_name_field[:readonly]
+
+    vap_code_field = find('#input1', match: :first)
+    assert_equal @billing_profile.vat_code, vap_code_field.value.empty? ? nil : @billing_profile.vat_code
+    assert vap_code_field[:readonly]
+
+    street_field = find('#input2', match: :first)
+    assert_equal @billing_profile.street, street_field.value
+    assert street_field[:readonly]
+
+    city_field = find('#input3', match: :first)
+    assert_equal @billing_profile.city, city_field.value
+    assert city_field[:readonly]
+
+    postal_code_field = find('#input4', match: :first)
+    assert_equal @billing_profile.postal_code, postal_code_field.value
+    assert postal_code_field[:readonly]
+
+    find("a.c-btn.c-btn--ghost.c-btn--icon[href='/billing_profiles/762b09cb-bf40-4cf7-bdab-09cf677d0d0f/edit']").click
+
+    # TODO:
+    # Here should be wot turbo request format
+
+    find('input[name="billing_profile[name]"]').set('Sasha Belyi')
+    find('input[name="billing_profile[street]"]').set('Paldiski mnt 12')
+    find('input[name="billing_profile[city]"]').set('Tallinn')
+    find('input[name="billing_profile[postal_code]"]').set('10137')
+    find('select[name="billing_profile[country_code]"]').select('Estonia')
+
+    find('.c-btn.c-btn--ghost.c-btn--icon.js-modal-delete-toggle').click
+
+    user_name_field = find('.o-card__title.c-account__title', match: :first)
+    assert_equal 'Sasha Belyi', user_name_field.value
+    assert user_name_field[:readonly]
+  end
+
+  def test_user_can_delete_his_billing_profile
+
+  end
 
   # def test_user_details_contains_a_link_to_billing
   #   visit user_path(@user.uuid)