def test_format_htaccess_file(self): """Tests correctly formatting a .htaccess file.""" # With trailing slash. obs = format_htaccess_file('/foo/bar/baz/', 'NAU123') self.assertEqual(obs, expected_htaccess_file) # Without trailing slash. obs = format_htaccess_file('/foo/bar/baz', 'NAU123') self.assertEqual(obs, expected_htaccess_file)
def generate_passwords(pids_f, results_dir, password_dir, out_dir): """Creates PID -> password mapping, .htaccess files, and .htpasswd file.""" htpasswd_f = open(join(out_dir, ".htpasswd"), "w") pid_passwd_f = open(join(out_dir, "personal_ids_with_passwords.txt"), "w") for line in pids_f: pid = line.strip() pid_dir = join(results_dir, pid) if not exists(pid_dir): raise ValueError( "The '%s' directory does not exist. Cannot " "create a password for an individual without " "results." % pid_dir ) htaccess_f = open(join(pid_dir, ".htaccess"), "w") htaccess_f.write(format_htaccess_file(password_dir, pid)) htaccess_f.close() password, encrypted_password = generate_random_password() htpasswd_f.write("%s:%s\n" % (pid, encrypted_password)) pid_passwd_f.write("%s\t%s\n" % (pid, password)) htpasswd_f.close() pid_passwd_f.close()