diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b0edaaf --- /dev/null +++ b/Gemfile @@ -0,0 +1,15 @@ +source 'https://rubygems.org' + +gem 'xmlconv', :path => '../xmlconv' +gem 'minitest' +gem 'flexmock' +group :debugger do + gem 'pry-byebug' + gem 'pry-doc' +end + +group :test do + gem 'rspec' + gem 'watir' + gem 'watir-webdriver' +end \ No newline at end of file diff --git a/spec/data/invoice.xml b/spec/data/invoice.xml new file mode 100644 index 0000000..ed43f9c --- /dev/null +++ b/spec/data/invoice.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spec/injection_spec.rb b/spec/injection_spec.rb new file mode 100644 index 0000000..24aecdd --- /dev/null +++ b/spec/injection_spec.rb @@ -0,0 +1,80 @@ +#!/usr/bin/env ruby +# encoding: utf-8 +require 'spec_helper' +@workThread = nil +CustomerId = 4100614181 +ProductName = 'Bilol 2.5 mg Filmtbl 100' +Here = File.expand_path(File.dirname(__FILE__)) + +describe "bbmb.xmlconv" do + + before :all do + @idx = 0 + waitForBbmbToBeReady(@browser, BbmbUrl) + end + + before :each do + @browser.goto BbmbUrl + end + + after :each do + @idx += 1 + createScreenshot(@browser, '_'+@idx.to_s) + logout + end + + after :all do + @browser.close + end + + describe "admin" do + before :all do + end + before :each do + @browser.goto BbmbUrl + logout + login(AdminUser, AdminPassword) + end + after :all do + logout + end + it "handle an injection properly" do + total_before, item_before = get_total_and_item_total + puts "Before #{total_before} item #{item_before}" + inject_invoice + # binding.pry + logout + login(AdminUser, AdminPassword) + total_after, item_after = get_total_and_item_total + puts "After #{total_after} item #{item_after}" + diff_total = total_after - total_before + diff_item= item_after - item_before + puts "diff_item #{diff_item} diff_total #{diff_total}" + end + end + + def inject_invoice + filename = File.join(Here, 'data', 'invoice.xml') + expect(File.exists?(filename)).to be true + cmd = "curl http://sandoz.xmlconv.bbmb.ngiger.ch/propharma -X POST -H 'Content-type: text/xml' --data @#{filename}" + expect(system(cmd)).to be true + end + + def get_total_and_item_total + @browser.goto "#{BbmbUrl}/de/#{Flavor}/customer/customer_id/#{CustomerId}" + windowSize = @browser.windows.size + expect(@browser.url).to match BbmbUrl + text = @browser.text.clone + expect(@browser.url).to match BbmbUrl + expect(@browser.url).to match CustomerId.to_s + how_much = @browser.link(:name => 'turnover').text + @turnover = how_much.match(/(\d+\.\d+)/)[1].to_f + @browser.link(:name => 'history').click + expect(@browser.url).to match BbmbUrl + expect(@browser.url).to match CustomerId.to_s + expect(@browser.url).to match 'history' + wieviel = @browser.text.match(/#{ProductName}\s+\d+\.\d+\s+(\d+\.\d+)/) + @item_turnover = wieviel[1].to_f + return @turnover, @item_turnover + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..c5eaa00 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,134 @@ +#!/usr/bin/env ruby +# encoding: utf-8 +# require 'simplecov' +# SimpleCov.start + +RSpec.configure do |config| + config.mock_with :flexmock + config.expect_with :rspec do |c| + c.syntax = [:should, :expect] + end +end + +BreakIntoPry = false +begin + require 'pry' +rescue LoadError + # ignore error for Travis-CI +end +$LOAD_PATH << File.join(File.dirname(File.dirname(__FILE__)), 'lib') + +require 'fileutils' +require 'watir-webdriver' +require 'pp' + +AdminPassword = ENV['SANDOZ_ADMIN_PASSWD'] +AdminUser = 'admin@sandoz.ch' +XmlConvUrl = 'http://sandoz.xmlconv.bbmb.ngiger.ch/' +BbmbUrl = 'http://sandoz.bbmb.ngiger.ch/' + +Flavor = 'sbsm' +ImageDest = File.join(Dir.pwd, 'images') +Browser2test = [ :chrome ] +DownloadDir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'downloads')) +GlobAllDownloads = File.join(DownloadDir, '*') +LeeresResult = /hat ein leeres Resultat/ + +def setup_browser + return if @browser + FileUtils.makedirs(DownloadDir) + if Browser2test[0].to_s.eql?('firefox') + puts "Setting upd default profile for firefox" + profile = Selenium::WebDriver::Firefox::Profile.new + profile['browser.download.dir'] = DownloadDir + profile['browser.download.folderList'] = 2 + profile['browser.helperApps.alwaysAsk.force'] = false + profile['browser.helperApps.neverAsk.saveToDisk'] = "application/zip;application/octet-stream;application/x-zip;application/x-zip-compressed;text/csv;test/semicolon-separated-values" + + @browser = Watir::Browser.new :firefox, :profile => profile + elsif Browser2test[0].to_s.eql?('chrome') + puts "Setting up a default profile for chrome" + prefs = { + :download => { + :prompt_for_download => false, + :default_directory => DownloadDir + } + } + @browser = Watir::Browser.new :chrome, :prefs => prefs + elsif Browser2test[0].to_s.eql?('ie') + puts "Trying unknown browser type Internet Explorer" + @browser = Watir::Browser.new :ie + else + puts "Trying unknown browser type #{Browser2test[0]}" + @browser = Watir::Browser.new Browser2test[0] + end +end + +def login(user = ViewerUser, password=ViewerPassword, remember_me=false) + setup_browser + @browser.goto BbmbUrl + sleep 0.5 + @browser.text_field(:name, 'email').when_present.set(user) + @browser.text_field(:name, 'pass').when_present.set(password) + @browser.button(:name,"login").click + sleep 1 unless @browser.button(:name,"logout").exists? + if @browser.button(:name,"login").exists? + @browser.goto(BbmbUrl) + return false + else + return true + end +end + +def get_session_timestamp + @@timestamp ||= Time.now.strftime('%Y%m%d_%H%M%S') +end + +def logout + setup_browser + @browser.goto BbmbUrl + sleep(0.1) unless @browser.link(:name=>'logout').exists? + logout_btn = @browser.link(:name=>'logout') + return unless logout_btn.exists? + logout_btn.click +end + +def waitForBbmbToBeReady(browser = nil, url = BbmbUrl, maxWait = 30) + setup_browser + startTime = Time.now + @seconds = -1 + 0.upto(maxWait).each{ + |idx| + @browser.goto BbmbUrl; small_delay + unless /Es tut uns leid/.match(@browser.text) + @seconds = idx + break + end + if idx == 0 + $stdout.write "Waiting max #{maxWait} seconds for #{url} to be ready"; $stdout.flush + else + $stdout.write('.'); $stdout.flush + end + sleep 1 + } + endTime = Time.now + sleep(0.2) + @browser.link(:text=>'Plus').click if @browser.link(:text=>'Plus').exists? + puts "Took #{(endTime - startTime).round} seconds for for #{BbmbUrl} to be ready. First answer was after #{@seconds} seconds." if (endTime - startTime).round > 2 +end + +def small_delay + sleep(0.1) +end + +def createScreenshot(browser, added=nil) + small_delay + if browser.url.index('?') + name = File.join(ImageDest, File.basename(browser.url.split('?')[0]).gsub(/\W/, '_')) + else + name = File.join(ImageDest, browser.url.split('/')[-1].gsub(/\W/, '_')) + end + name = "#{name}#{added}.png" + browser.screenshot.save (name) + puts "createScreenshot: #{name} done" if $VERBOSE +end diff --git a/test/test_conversion/propharma_bdd.rb b/test/test_conversion/propharma_bdd.rb index 790a442..9de78d4 100644 --- a/test/test_conversion/propharma_bdd.rb +++ b/test/test_conversion/propharma_bdd.rb @@ -4,13 +4,13 @@ $: << File.expand_path('..', File.dirname(__FILE__)) $: << File.expand_path('../../lib', File.dirname(__FILE__)) -require 'test/unit' require 'conversion/propharma_bdd' -require 'flexmock' +require 'minitest/autorun' +require 'flexmock/minitest' module XmlConv module Conversion - class TestWbmbBdd < Test::Unit::TestCase + class TestWbmbBdd < ::Minitest::Test def setup @src = <<-EOS [KUNDE] @@ -68,10 +68,6 @@ def setup mge=2 EOS end - def test_parse - ast = ProPharmaBdd.parse(@src) - assert_instance_of(SyntaxTree, ast) - end def test_convert bdd = ProPharmaBdd.convert(ProPharmaBdd.parse(@src)) assert_instance_of(Model::Bdd, bdd) diff --git a/test/test_conversion/sunstore_bdd.rb b/test/test_conversion/sunstore_bdd.rb index 2984f07..549f924 100644 --- a/test/test_conversion/sunstore_bdd.rb +++ b/test/test_conversion/sunstore_bdd.rb @@ -4,13 +4,13 @@ $: << File.dirname(__FILE__) $: << File.expand_path('../../lib', File.dirname(__FILE__)) -require 'test/unit' require 'conversion/sunstore_bdd' require 'xmlconv/util/transaction' +require 'minitest/autorun' module XmlConv module Conversion - class TestGehBdd < Test::Unit::TestCase + class TestGehBdd < ::Minitest::Test def setup @src = <<-XML @@ -73,7 +73,7 @@ def test_convert "Linkestrasse 99" ], address.lines) assert_equal("3322", address.zip_code) - assert_equal("Sch\366nb\374hl", address.city) + assert_equal("Schönbühl", address.city) assert_equal(2, delivery.items.size) item = delivery.items.first diff --git a/test/test_integration/propharma_csv.rb b/test/test_integration/propharma_csv.rb index 6fcb27a..cc4f874 100644 --- a/test/test_integration/propharma_csv.rb +++ b/test/test_integration/propharma_csv.rb @@ -4,15 +4,15 @@ $: << File.expand_path('..', File.dirname(__FILE__)) $: << File.expand_path('../../lib', File.dirname(__FILE__)) -require 'test/unit' require 'xmlconv/util/transaction' require 'xmlconv/util/destination' require 'conversion/propharma_bdd' require 'conversion/bdd_csv' +require 'minitest/autorun' module XmlConv module Integration -class TestProPharmaI2 < Test::Unit::TestCase +class TestProPharmaI2 < ::Minitest::Test def setup @target_dir = File.expand_path('data/propharma_csv', File.dirname(__FILE__)) diff --git a/test/test_postprocess/bbmb2.rb b/test/test_postprocess/bbmb2.rb index b56b283..3376871 100644 --- a/test/test_postprocess/bbmb2.rb +++ b/test/test_postprocess/bbmb2.rb @@ -3,16 +3,16 @@ $: << File.expand_path('../../lib', File.dirname(__FILE__)) -require 'test/unit' require 'postprocess/bbmb2' -require 'flexmock' require 'conversion/pharmacieplus_bdd' require 'conversion/propharma_bdd' require 'conversion/wbmb_bdd' +require 'minitest/autorun' +require 'flexmock/minitest' module XmlConv module PostProcess - class TestBbmb2 < Test::Unit::TestCase + class TestBbmb2 < ::Minitest::Test def test_inject src = <<-XML @@ -104,7 +104,7 @@ def test_inject Pharmacie du Mandement - Monsieur Frédéric Recipient + Monsieur Frédéric Recipient
3e adresse e-mail 1242 @@ -135,13 +135,13 @@ def test_inject - Pharm. Ecole-de-Médecine + Pharm. Ecole-de-Médecine - Madame Françoise Recipient + Madame Françoise Recipient
3e adresse e-mail 1205 - Genève + Genève
@@ -219,7 +219,7 @@ def test_inject__soap name - ywesee Testspitäler + ywesee Testspitäler kundennummer