Beispiel #1
0
 def test_change_names(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     sqlauth.add_user("test_user", "123", fullname="Test User")
     guid = sqlauth.get_guid("test_user")
     sqlauth.modify_user("test_user", name_new="test_2", fullname="Test 2")
     with sqlauth.session_scope() as session:
         user = session.query(User).filter_by(id=guid).scalar()
         self.assertEqual(user.name, "test_2")
         self.assertEqual(user.fullname, "Test 2")
Beispiel #2
0
 def test_authorized(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     sqlauth.add_user("test_user", "123", fullname="Test User")
     self.assertTrue(sqlauth.authorized("test_user", "123"))
     self.assertFalse(sqlauth.authorized("test_user", "1234"))
     self.assertFalse(sqlauth.authorized("not_exist", "123"))
 def test_permissions(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     sqlauth.add_user("test_owner", "123", role=ROLE_OWNER)
     sqlauth.add_user("test_guest", "123", role=ROLE_GUEST)
     self.assertIn(PERM_DEL_USER, sqlauth.get_permissions("test_owner"))
     self.assertNotIn(PERM_DEL_USER, sqlauth.get_permissions("test_guest"))
     self.assertIn(PERM_EDIT_OWN_USER,
                   sqlauth.get_permissions("test_owner"))
     self.assertIn(PERM_EDIT_OWN_USER,
                   sqlauth.get_permissions("test_guest"))
Beispiel #4
0
 def test_add_user(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     with self.assertRaises(IntegrityError):
         sqlauth.add_user(None, "123")  # NULL username
     with self.assertRaises(ValueError):
         sqlauth.add_user("", "123")  # empty username
     with self.assertRaises(ValueError):
         sqlauth.add_user("test_user", "")  # empty pw
     sqlauth.add_user("test_user", "123", fullname="Test User")
     with self.assertRaises(IntegrityError):
         # adding again should fail
         sqlauth.add_user("test_user", "123", fullname="Test User")
     with sqlauth.session_scope() as session:
         user = session.query(User).filter_by(name="test_user").scalar()
         self.assertEqual(user.name, "test_user")
         self.assertEqual(user.fullname, "Test User")
Beispiel #5
0
 def test_delete_user(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     sqlauth.add_user("test_user", "123", fullname="Test User")
     with sqlauth.session_scope() as session:
         user = session.query(User).filter_by(name="test_user").scalar()
         self.assertIsNotNone(user)
         sqlauth.delete_user("test_user")
         user = session.query(User).filter_by(name="test_user").scalar()
         self.assertIsNone(user)
     with self.assertRaisesRegex(ValueError, r".* not found"):
         # deleting again should fail
         sqlauth.delete_user("test_user")
 def test_add_user(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     assert sqlauth.config_get_all() == {}
     assert sqlauth.config_get("nope") is None
     with self.assertRaises(ValueError):
         sqlauth.config_set("key1", "value1")
     sqlauth.config_set("EMAIL_HOST", "value1")
     assert sqlauth.config_get("EMAIL_HOST") == "value1"
     sqlauth.config_set("EMAIL_HOST", "value2")
     assert sqlauth.config_get("EMAIL_HOST") == "value2"
     sqlauth.config_set("EMAIL_HOST", 1)
     assert sqlauth.config_get("EMAIL_HOST") == "1"
     sqlauth.config_set("EMAIL_PORT", 2)
     assert sqlauth.config_get("EMAIL_PORT") == "2"
     sqlauth.config_delete("EMAIL_HOST")
     assert sqlauth.config_get("EMAIL_HOST") is None
     assert sqlauth.config_get("EMAIL_PORT") == "2"
     assert sqlauth.config_get("nope") is None
     sqlauth.config_delete("nope")
     assert sqlauth.config_get("nope") is None
 def test_count_users(self):
     sqlauth = SQLAuth("sqlite://", logging=False)
     sqlauth.create_table()
     assert sqlauth.get_number_users() == 0
     sqlauth.add_user("user1", "123", role=1)
     sqlauth.add_user("user2", "123", role=1)
     sqlauth.add_user("user3", "123", role=3)
     sqlauth.add_user("user4", "123", role=4)
     assert sqlauth.get_number_users() == 4
     assert sqlauth.get_number_users((1, )) == 2
     assert sqlauth.get_number_users((2, )) == 0
     assert sqlauth.get_number_users((1, 3)) == 3