<commit_before>require "selenium-webdriver"
require 'json'

require_relative '../../utils/sli_utils.rb'
require_relative '../../utils/selenium_common.rb'

When /^I hit the Admin Application Authorization Tool$/ do
  #XXX - Once the API is ready, remove the ID
  @driver.get(PropLoader.getProps['admintools_server_url']+"/application_authorizations/")
end

When /^I was redirected to the "([^"]*)" IDP Login page$/ do |arg1|
  assertWithWait("Failed to navigate to the IDP Login page")  {@driver.find_element(:id, "IDToken1")}
end

Then /^I am redirected to the Admin Application Authorization Tool$/ do
  assertWithWait("Failed to navigate to the Admintools App Registration page")  {@driver.page_source.index("application_authorizations") != nil}
end

Then /^in the upper right corner I see my name$/ do
  assertWithWait("Failed to find name in upper right corner") {@driver.page_source.index("Sunset Admin") != nil}
end

Then /^I see a label in the middle "([^"]*)"/ do |arg1|
  #We're changing how the ID is referenced, so the label for the time-being isn't going to be accurate
  #assert(@driver.page_source.index(arg1) != nil)
end

Then /^I see the list of all available apps on SLI$/ do
  @appsTable = @driver.find_element(:id, "Authorized Apps Table")
  assert(@appsTable != nil  )
end

Then /^the authorized apps for my district are colored green$/ do
  approved = @appsTable.find_elements(:xpath, ".//tr/td[text()='Approved']")
  approved.each do |currentStatus|
    assert(currentStatus.attribute(:id) == "approvedStatus", "App is not the right color, should be green")
  end
end

Then /^the unauthorized are colored red$/ do
  notApproved = @appsTable.find_elements(:xpath, ".//tr/td[text()='Not Approved']")
  notApproved.each do |currentStatus|
    assert(currentStatus.attribute(:id) == "notApprovedStatus", "App is not the right color, should be red")
  end
end

Then /^are sorted by 'Status'$/ do
  tableHeadings = @appsTable.find_elements(:xpath, ".//tr/th")
  index = 0
  tableHeadings.each do |arg|
    index = tableHeadings.index(arg) + 1 if arg.text == "Status"    
  end
  rows = @appsTable.find_elements(:xpath, ".//tr/..")
  inApprovedSection = true
  rows.each do |curRow| 
    td = curRow.find_element(:xpath, ".//td[#{index}]")
    assert(inApprovedSection || (!inApprovedSection && td.text != "Approved"), "Encountered an app with a 'Approved' status after one with a 'Not Approved' status")
    if td.text == "Not Approved"
      inApprovedSection = false
    end
  end
end

Then /^I see the Name, Version, Vendor and Status of the apps$/ do
  expectedHeadings = ["Name", "Version", "Vendor", "Status", ""]
  tableHeadings = @appsTable.find_elements(:xpath, ".//tr/th")
  actualHeadings = []
  tableHeadings.each do |heading|
    actualHeadings.push(heading.text)
  end    
  assert(expectedHeadings.sort == actualHeadings.sort, "Headings are different, found #{actualHeadings.inspect} but expected #{expectedHeadings.inspect}")
end

Given /^I am a valid SEA\/LEA user$/ do
end

When /^I hit the Application Authorization Tool$/ do
  pending # express the regexp above with the code you wish you had
end

When /^the login process is initiated$/ do
  pending # express the regexp above with the code you wish you had
end

When /^I pass my valid username and password$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^I get message that I am not authorized$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^I am not logged into the application$/ do
  step "I hit the Admin Application Authorization Tool"
end


Given /^I am logged into the Application Authorization Tool$/ do
end

Given /^I see an application "([^"]*)" in the table$/ do |arg1|
  @appName = arg1
  apps = @driver.find_elements(:xpath, ".//tr/td[text()='#{arg1}']/..")
  assert(apps != nil)
end

Given /^in Status it says "([^"]*)"$/ do |arg1|
  headings = @driver.find_elements(:xpath, ".//tr/th")
  index = 0
  headings.each do |heading|
    if heading.text == "Status"
      index = headings.index(heading) + 1
    end
  end
  
  @appRow = @driver.find_element(:xpath, ".//tr/td[text()='#{@appName}']/..")
  actualStatus = @appRow.find_element(:xpath, ".//td[#{index}]").text<commit_msg>step def to verify not authorized now implemented<commit_after>require "selenium-webdriver"
require 'json'

require_relative '../../utils/sli_utils.rb'
require_relative '../../utils/selenium_common.rb'

When /^I hit the Admin Application Authorization Tool$/ do
  #XXX - Once the API is ready, remove the ID
  @driver.get(PropLoader.getProps['admintools_server_url']+"/application_authorizations/")
end

When /^I was redirected to the "([^"]*)" IDP Login page$/ do |arg1|
  assertWithWait("Failed to navigate to the IDP Login page")  {@driver.find_element(:id, "IDToken1")}
end

Then /^I am redirected to the Admin Application Authorization Tool$/ do
  assertWithWait("Failed to navigate to the Admintools App Registration page")  {@driver.page_source.index("application_authorizations") != nil}
end

Then /^in the upper right corner I see my name$/ do
  assertWithWait("Failed to find name in upper right corner") {@driver.page_source.index("Sunset Admin") != nil}
end

Then /^I see a label in the middle "([^"]*)"/ do |arg1|
  #We're changing how the ID is referenced, so the label for the time-being isn't going to be accurate
  #assert(@driver.page_source.index(arg1) != nil)
end

Then /^I see the list of all available apps on SLI$/ do
  @appsTable = @driver.find_element(:id, "Authorized Apps Table")
  assert(@appsTable != nil  )
end

Then /^the authorized apps for my district are colored green$/ do
  approved = @appsTable.find_elements(:xpath, ".//tr/td[text()='Approved']")
  approved.each do |currentStatus|
    assert(currentStatus.attribute(:id) == "approvedStatus", "App is not the right color, should be green")
  end
end

Then /^the unauthorized are colored red$/ do
  notApproved = @appsTable.find_elements(:xpath, ".//tr/td[text()='Not Approved']")
  notApproved.each do |currentStatus|
    assert(currentStatus.attribute(:id) == "notApprovedStatus", "App is not the right color, should be red")
  end
end

Then /^are sorted by 'Status'$/ do
  tableHeadings = @appsTable.find_elements(:xpath, ".//tr/th")
  index = 0
  tableHeadings.each do |arg|
    index = tableHeadings.index(arg) + 1 if arg.text == "Status"    
  end
  rows = @appsTable.find_elements(:xpath, ".//tr/..")
  inApprovedSection = true
  rows.each do |curRow| 
    td = curRow.find_element(:xpath, ".//td[#{index}]")
    assert(inApprovedSection || (!inApprovedSection && td.text != "Approved"), "Encountered an app with a 'Approved' status after one with a 'Not Approved' status")
    if td.text == "Not Approved"
      inApprovedSection = false
    end
  end
end

Then /^I see the Name, Version, Vendor and Status of the apps$/ do
  expectedHeadings = ["Name", "Version", "Vendor", "Status", ""]
  tableHeadings = @appsTable.find_elements(:xpath, ".//tr/th")
  actualHeadings = []
  tableHeadings.each do |heading|
    actualHeadings.push(heading.text)
  end    
  assert(expectedHeadings.sort == actualHeadings.sort, "Headings are different, found #{actualHeadings.inspect} but expected #{expectedHeadings.inspect}")
end

Given /^I am a valid SEA\/LEA user$/ do
end

When /^I hit the Application Authorization Tool$/ do
  pending # express the regexp above with the code you wish you had
end

When /^the login process is initiated$/ do
  pending # express the regexp above with the code you wish you had
end

When /^I pass my valid username and password$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^I get message that I am not authorized$/ do
  isForbidden = @driver.find_element(:xpath, './/body/title[text()="Not Authorized (403)"]')
  assert(isForbidden != nil)
end

Then /^I am not logged into the application$/ do
  step "I hit the Admin Application Authorization Tool"
end


Given /^I am logged into the Application Authorization Tool$/ do
end

Given /^I see an application "([^"]*)" in the table$/ do |arg1|
  @appName = arg1
  apps = @driver.find_elements(:xpath, ".//tr/td[text()='#{arg1}']/..")
  assert(apps != nil)
end

Given /^in Status it says "([^"]*)"$/ do |arg1|
  headings = @driver.find_elements(:xpath, ".//tr/th")
  index = 0
  headings.each do |heading|
    if heading.text == "Status"
      index = headings.index(heading) + 1
    end
  end
  
  @appRow = @driver.find_element(:xpath, ".//tr/td[text()='#{@appName}']/..")
  actualStatus = @appRow.find_element(:xpath, ".//td[#{index}]").text
{'fraction_non_alphanumeric': 0.13692415076779899, 'fraction_numerical': 0.003140995812005584, 'mean_word_length': 4.077968103957472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6820897', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2726, 'n_words': 945}