ECP-170 Remove login requirement for Trition. Switch to using predefined passwords stored in the database for district login.
parent
72e38f5ee8
commit
2068758ae4
@ -0,0 +1,45 @@
|
||||
require "csv"
|
||||
|
||||
class CredentialsLoader
|
||||
def self.load_credentials(file:)
|
||||
credentials = []
|
||||
CSV.parse(file, headers: true) do |row|
|
||||
values = CredentialRowValues.new(row:)
|
||||
next unless values.district.present?
|
||||
|
||||
credentials << values.district
|
||||
end
|
||||
District.import(credentials, batch_size: 100, on_duplicate_key_update: [:username, :password, :login_required])
|
||||
end
|
||||
end
|
||||
|
||||
class CredentialRowValues
|
||||
attr_reader :row
|
||||
|
||||
def initialize(row:)
|
||||
@row = row
|
||||
end
|
||||
|
||||
def district
|
||||
@district ||= begin
|
||||
name = row["Districts"]&.strip
|
||||
district = District.find_or_initialize_by(name:)
|
||||
district.username = username
|
||||
district.password = password
|
||||
district.login_required = login_required?
|
||||
district
|
||||
end
|
||||
end
|
||||
|
||||
def username
|
||||
row["Username"]&.strip
|
||||
end
|
||||
|
||||
def password
|
||||
row["PW"]&.strip
|
||||
end
|
||||
|
||||
def login_required?
|
||||
row["Login Required"]&.strip == "Y"
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,17 @@
|
||||
require 'net/sftp'
|
||||
require 'uri'
|
||||
|
||||
module Sftp
|
||||
class File
|
||||
def self.open(filepath:, &block)
|
||||
sftp_url = ENV['SFTP_URL']
|
||||
uri = URI.parse(sftp_url)
|
||||
Net::SFTP.start(uri.host, uri.user, password: uri.password) do |sftp|
|
||||
sftp.file.open(filepath, 'r', &block)
|
||||
end
|
||||
rescue Net::SFTP::StatusException => e
|
||||
puts "Error opening file: #{e.message}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,7 @@
|
||||
class AddCredentialsToDistrict < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :districts, :username, :string, null: true, default: nil
|
||||
add_column :districts, :password, :string, null: true, default: nil
|
||||
add_column :districts, :login_required, :boolean, null: false, default: true
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,7 @@
|
||||
class AddNameSlugAndQualtricsCodeIndexesToDistrict < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_index :districts, :name, unique: true
|
||||
add_index :districts, :slug, unique: true
|
||||
add_index :districts, :qualtrics_code, unique: true
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,37 @@
|
||||
require "rails_helper"
|
||||
require "fileutils"
|
||||
|
||||
RSpec.describe CredentialsLoader do
|
||||
let(:path) do
|
||||
Rails.root.join("spec", "fixtures", "credentials", "credentials.csv")
|
||||
end
|
||||
|
||||
context ".load_credentials" do
|
||||
before do
|
||||
create(:district, name: "Maynard Public Schools")
|
||||
create(:district, name: "Springfield Public Schools")
|
||||
create(:district, name: "Boston Public Schools")
|
||||
end
|
||||
|
||||
it "loads credentials from the CSV file into the database" do
|
||||
file = File.open(Rails.root.join("spec", "fixtures", "sample_district_credentials.csv"))
|
||||
# Seeder.new.seed_district_credentials(file:)
|
||||
expect { CredentialsLoader.load_credentials(file:) }.to change { District.count }.by(0)
|
||||
|
||||
district = District.find_by(name: "Maynard Public Schools")
|
||||
expect(district.username).to eq("maynard_admin")
|
||||
expect(district.password).to eq("password123")
|
||||
expect(district.login_required).to be true
|
||||
|
||||
district = District.find_by(name: "Springfield Public Schools")
|
||||
expect(district.username).to eq("springfield_admin")
|
||||
expect(district.password).to eq("password456")
|
||||
expect(district.login_required).to be false
|
||||
|
||||
district = District.find_by(name: "Boston Public Schools")
|
||||
expect(district.username).to eq("boston_admin")
|
||||
expect(district.password).to eq("password789")
|
||||
expect(district.login_required).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,7 +1,7 @@
|
||||
module BasicAuthHelper
|
||||
def login_as(district)
|
||||
user = district.short_name
|
||||
pw = "#{user}!"
|
||||
user = district.username
|
||||
pw = district.password
|
||||
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user, pw)
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in new issue