def setup_method(self, method): db.drop_all() db.create_all() # Create admin user for tests. admin = Users(id = "adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. user = Users(id = "testuser") user.first_name = "Test1" user.last_name = "User" user.email = "*****@*****.**" user.is_admin = False db.session.add(user) db.session.commit() self.test_user = user self.user_token = user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create normal user for tests. user2 = Users(id = "testuser2") user2.first_name = "Test" user2.last_name = "User" user2.email = "*****@*****.**" user2.is_admin = False db.session.add(user2) db.session.commit() self.test_user2 = user2 self.user_token2 = user2.generate_auth() self.user_token2 = self.user_token2.decode('ascii') # Create a test committee. committee = Committees(id = "testcommittee") committee.title = "Test Committee" committee.description = "Test Description" committee.location = "Test Location" committee.meeting_time = "1300" committee.meeting_day = 2 committee.head = "testuser" self.committee = committee db.session.add(committee) db.session.commit() committee_note = CommitteeNotes(id = 10) committee_note.author = "testuser" committee_note.description = "Test Note" committee_note.committee = "testcommittee" committee_note.hidden = False self.committee_note = committee_note db.session.add(self.committee_note) db.session.commit()
def setup_method(self, method): db.drop_all() db.create_all() self.user_data = { "user_id": "testuser", "committee_id": "testcommittee" } # Create admin user for tests. admin = Users(id="adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. user = Users(id="testuser") user.first_name = "Test1" user.last_name = "User" user.email = "*****@*****.**" user.is_admin = False db.session.add(user) db.session.commit() self.user_token = user.generate_auth() self.user_token = self.user_token.decode('ascii') self.user = user # Create second normal user for tests. user2 = Users(id="testuser2") user2.first_name = "Test2" user2.last_name = "User2" db.session.add(user2) db.session.commit() self.user_token2 = user2.generate_auth() self.user_token2 = self.user_token2.decode('ascii') self.user2 = user2 # Create a test committee. self.committee = Committees(id="testcommittee") self.committee.title = "Test Committee" self.committee.description = "Test Description" self.committee.location = "Test Location" self.committee.meeting_time = "1300" self.committee.meeting_day = 2 self.committee.head = "adminuser" db.session.add(self.committee) db.session.commit() self.invite = Invitations(id=5) self.invite.user_name = "newuser1" self.invite.committee = self.committee self.invite.committee_id = "testcommittee" self.invite.isInvite = False
def setup_method(self, method): db.drop_all() db.create_all() self.test_committee_dict = { "id": "testcommittee", "title": "testcommittee", "description": "Test Description", "location": "Test Location", "meeting_time": "1300", "meeting_day": 2, "head": "testuser", "committee_img": "R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" } self.test_committee = Committees(id="testcommittee") self.test_committee.title = self.test_committee_dict["title"] self.test_committee.location = self.test_committee_dict["location"] self.test_committee.description = self.test_committee_dict[ "description"] self.test_committee.meeting_time = self.test_committee_dict[ "meeting_time"] self.test_committee.meeting_day = self.test_committee_dict[ "meeting_day"] self.test_committee.head = self.test_committee_dict["head"] com_img = base64.b64decode(self.test_committee_dict["committee_img"]) self.test_committee.com_image = com_img # Create admin user for tests. admin = Users(id="adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. user = Users(id="testuser") user.first_name = "Test1" user.last_name = "User" user.email = "*****@*****.**" user.is_admin = False db.session.add(user) db.session.commit() self.test_user = user self.user_token = user.generate_auth() self.user_token = self.user_token.decode('ascii')
def login_ldap(credentials): ldap_server = "ldaps://ldap.rit.edu" if credentials.get("username", "") == "" or credentials.get( "password", "") == "": emit('auth', {'error': "Authentication error."}) return user_dn = "uid=" + credentials.get("username", "") + ",ou=People,dc=rit,dc=edu" search_filter = "uid=" + credentials.get("username", "") connect = ldap.initialize(ldap_server) try: connect.bind_s(user_dn, credentials.get("password", "")) result = connect.search_s(user_dn, ldap.SCOPE_SUBTREE, search_filter) connect.unbind_s() values = result[0][1] username = values["uid"][0].decode('utf-8') firstname = values["givenName"][0].decode('utf-8') lastname = values["sn"][0].decode('utf-8') email = values["mail"][0].decode('utf-8') # Check if a user exists. if Users.query.filter_by(id=username).first() is not None: user = Users.query.filter_by(id=username).first() token = user.generate_auth() admin = user.is_admin emit( 'auth', { 'token': token.decode('ascii'), 'admin': admin, 'super': user.is_super, 'username': username }) else: user = Users(id=username) user.first_name = firstname user.last_name = lastname user.email = email db.session.add(user) db.session.commit() token = user.generate_auth() emit('auth', {'token': token.decode('ascii')}) except ldap.LDAPError: connect.unbind_s() emit('auth', Response.AuthError) return
class TestUser(object): @classmethod def setup_class(self): self.app = app.test_client() app.config['TESTING'] = True app.config['SECRET_KEY'] = "test_key" app.config[ 'SQLALCHEMY_DATABASE_URI'] = config.SQLALCHEMY_TEST_DATABASE_URI db = SQLAlchemy(app) db.session.close() db.drop_all() db.create_all() self.socketio = socketio.test_client(app) self.socketio.connect() @classmethod def setup_method(self, method): db.drop_all() db.create_all() # Create normal user for tests. self.user = Users(id="testuser") self.user.first_name = "Test1" self.user.last_name = "User" self.user.email = "*****@*****.**" self.user.is_admin = False db.session.add(self.user) db.session.commit() self.user_token = self.user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create admin user for tests. self.admin_user = Users(id="adminuser") self.admin_user.first_name = "Admin" self.admin_user.last_name = "User" self.admin_user.email = "*****@*****.**" self.admin_user.is_admin = True db.session.add(self.admin_user) db.session.commit() self.admin_user_token = self.admin_user.generate_auth() self.admin_user_token = self.admin_user_token.decode('ascii') @classmethod def teardown_class(self): db.session.close() db.drop_all() self.socketio.disconnect() # Test empty login. def test_empty_login(self): self.socketio.emit("auth", {"username": "", "password": ""}) received = self.socketio.get_received() assert received[0]["args"][0] == Response.AuthError # Test invalid datatype def test_invalid_datatype(self): self.socketio.emit("auth", "invalid_datatype") received = self.socketio.get_received() assert received[0]["args"][0] == {'error': 'Please check data type.'} # Test incorrect login. def test_incorrect_login(self): self.socketio.emit("auth", { "username": "******", "password": "******" }) received = self.socketio.get_received() assert received[0]["args"][0] == Response.AuthError # Correct login new user. @patch('ldap.initialize') def test_correct_login_new(self, mock_obj): # Prepare mock ldap. self.mocked_inst = mock_obj.return_value self.mocked_inst.bind_s = MagicMock() # Create expected ldap result. result_ldap = [('', { 'uid': [b'test'], 'givenName': [b'Test'], 'sn': [b'User'], 'mail': [b'*****@*****.**'] })] self.mocked_inst.search_s.return_value = result_ldap self.socketio.emit("auth", { "username": "******", "password": "******" }) received = self.socketio.get_received() assert 'token' in received[0]["args"][0] # Correct login existing user. @patch('ldap.initialize') def test_correct_login(self, mock_obj): # Prepare mock ldap. self.mocked_inst = mock_obj.return_value self.mocked_inst.bind_s = MagicMock() # Create expected ldap result. result_ldap = [('', { 'uid': [b'test'], 'givenName': [b'Test'], 'sn': [b'User'], 'mail': [b'*****@*****.**'] })] self.mocked_inst.search_s.return_value = result_ldap self.socketio.emit("auth", { "username": "******", "password": "******" }) # Request existing user. received = self.socketio.get_received() assert 'token' in received[0]["args"][0] @patch('ldap.initialize') def test_token_authentication(self, mock_obj): # Prepare mock ldap. self.mocked_inst = mock_obj.return_value self.mocked_inst.bind_s = MagicMock() # Create expected ldap result. result_ldap = [('', { 'uid': [b'test'], 'givenName': [b'Test'], 'sn': [b'User'], 'mail': [b'*****@*****.**'] })] self.mocked_inst.search_s.return_value = result_ldap self.socketio.emit("auth", { "username": "******", "password": "******" }) # Request token. received = self.socketio.get_received() token = received[0]["args"][0]["token"] # Test verify_auth when user doesnt exist. def test_verify_user_doesnt_exist(self): invalid_token = {"token": "thisisinvalid"} self.socketio.emit("verify_auth", invalid_token) received = self.socketio.get_received() assert received[0]["args"][0] == Response.AuthError # Test error in shibboleth login. def test_login_shib_error(self): with app.app_context(): res = login_from_acs({'errors': "thisisanerror"}) assert res.get_json() == {'errors': 'thisisanerror'} # Test shibboleth login failed. def test_login_shib_failed(self): with app.app_context(): res = login_from_acs({}) assert res.get_json() == {'error': 'login failed'} def test_login_shib_success(self): with app.test_request_context(): attributes = [('', ['']), ('', ['']), ('', ['']), ('', [''])] res = login_from_acs({'logged_in': True, 'attributes': attributes}) assert res.status == "302 FOUND" def test_get_all_users(self): return_data = [{ "username": self.user.id, "name": self.user.first_name + " " + self.user.last_name, }, { "username": self.admin_user.id, "name": self.admin_user.first_name + " " + self.admin_user.last_name }] self.socketio.emit("get_all_users") received = self.socketio.get_received() assert received[0]["args"][0] == return_data # Test edit roles without being an admin. def test_edit_roles_no_permissions(self): self.socketio.emit( "edit_roles", { "token": self.user_token, "username": "******", "role": Roles.AdminUser.value }) received = self.socketio.get_received() assert received[0]["args"][0] == Response.PermError # Test edit role of non-existing user. def test_edit_roles_user_not_found(self): self.socketio.emit( "edit_roles", { "token": self.admin_user_token, "username": "******", "role": Roles.AdminUser.value }) received = self.socketio.get_received() assert received[0]["args"][0] == Response.UserNotFound # Test edit non existing role. def test_edit_role_not_found(self): self.socketio.emit( "edit_roles", { "token": self.admin_user_token, "username": "******", "role": "NonExistingRole" }) received = self.socketio.get_received() assert received[0]["args"][0] == Response.RoleNotFound # Test set roles to manager user. def test_set_to_manager(self): self.socketio.emit( "edit_roles", { "token": self.admin_user_token, "username": "******", "role": Roles.ManagerUser.value }) received = self.socketio.get_received() assert received[0]["args"][0] == { "success": "Role set to ManagerUser." } # Test manager shouldnt edit roles. def test_manager_cant_edit_role(self): self.socketio.emit( "edit_roles", { "token": self.user_token, "username": "******", "role": Roles.ManagerUser.value }) received = self.socketio.get_received() assert received[0]["args"][0] == Response.PermError # Test set manager to admin user. def test_set_to_admin(self): self.socketio.emit( "edit_roles", { "token": self.admin_user_token, "username": "******", "role": Roles.AdminUser.value }) received = self.socketio.get_received() assert received[0]["args"][0] == {"success": "Role set to AdminUser."} # Test set admin to normal user. def test_set_to_normal(self): # Make testuser an admin. self.socketio.emit( "edit_roles", { "token": self.admin_user_token, "username": "******", "role": Roles.AdminUser.value }) # Make admin a normal user. self.socketio.emit( "edit_roles", { "token": self.user_token, "username": "******", "role": Roles.NormalUser.value }) received = self.socketio.get_received() assert received[1]["args"][0] == {"success": "Role set to NormalUser."}
def setup_method(self, method): db.drop_all() db.create_all() # Create admin user for tests. admin = Users(id="adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. user = Users(id="testuser") user.first_name = "Test1" user.last_name = "User" user.email = "*****@*****.**" user.is_admin = False db.session.add(user) db.session.commit() self.test_user = user self.user_token = user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create normal user for tests. user2 = Users(id="testuser2") user2.first_name = "Test" user2.last_name = "User" user2.email = "*****@*****.**" user2.is_admin = False db.session.add(user2) db.session.commit() self.test_user2 = user2 self.user_token2 = user2.generate_auth() self.user_token2 = self.user_token2.decode('ascii') # Create normal user for tests. user3 = Users(id="activemember") user3.first_name = "Active" user3.last_name = "Member" user3.email = "*****@*****.**" user3.is_admin = False db.session.add(user2) db.session.commit() self.test_user3 = user3 self.user_token3 = user3.generate_auth() self.user_token3 = self.user_token3.decode('ascii') # Create a test committee. committee = Committees(id="testcommittee") committee.title = "Test Committee" committee.description = "Test Description" committee.location = "Test Location" committee.meeting_time = "1300" committee.meeting_day = 2 committee.head = "testuser" self.committee = committee # Create a test committee. committee2 = Committees(id="testcommittee2") committee2.title = "Test Committee2" committee2.description = "Test Description" committee2.location = "Test Location" committee2.meeting_time = "1300" committee2.meeting_day = 2 committee2.head = "testuser" self.committee2 = committee2 # Add user3 to committee. role = Members(role=Roles.ActiveMember) role.member = self.test_user3 self.committee.members.append(role) db.session.add(self.committee) db.session.add(self.committee2) db.session.commit() self.charge_dict = { 'id': 10, 'title': 'Test Charge', 'description': 'Test Description', 'committee': 'testcommittee', 'status': 0, 'priority': 0, 'private': True, 'paw_links': "https://testlink.com" } # Create a test charge. charge = Charges(id=10) charge.author = "testuser" charge.title = "Test Charge" charge.description = "Test Description" charge.committee = "testcommittee" charge.paw_links = "https://testlink.com" charge.priority = 0 charge.status = 0 charge.private = True self.charge = charge db.session.add(charge) db.session.commit() self.charge_dict["created_at"] = self.charge.created_at.isoformat()
def setup_method(self, method): db.drop_all() db.create_all() self.socketio = socketio.test_client(app) self.socketio.connect() db.event.remove(Committees, "after_insert", new_committee) db.event.remove(Actions, "after_insert", new_action) # Create admin user for tests. admin = Users(id="adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. user = Users(id="testuser") user.first_name = "Test1" user.last_name = "User" user.email = "*****@*****.**" user.is_admin = False db.session.add(user) db.session.commit() self.test_user = user self.user_token = user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create normal user for tests. user_two = Users(id="testuser2") user_two.first_name = "Test2" user_two.last_name = "User" user_two.email = "*****@*****.**" user_two.is_admin = False db.session.add(user_two) db.session.commit() self.test_user_two = user_two self.user_token_two = user_two.generate_auth() self.user_token_two = self.user_token_two.decode('ascii') # Create a test committee. committee = Committees(id="testcommittee") committee.title = "Test Committee" committee.description = "Test Description" committee.location = "Test Location" committee.meeting_time = "1300" committee.meeting_day = 2 committee.head = "testuser" self.committee = committee db.session.add(committee) db.session.commit() # Create a test charge. charge = Charges(id=10) charge.author = "testuser" charge.title = "Test Charge" charge.description = "Test Description" charge.committee = "testcommittee" charge.paw_links = "https://testlink.com" charge.priority = 0 charge.status = 0 self.charge = charge db.session.add(charge) db.session.commit() # Create a test action. action = Actions(id=10) action.author = admin.id action.title = "Test Action" action.description = "Test Description" action.charge = 10 action.status = 0 self.test_action = action db.session.add(self.test_action) db.session.commit() db.event.listen(Committees, "after_insert", new_committee) db.event.listen(Actions, "after_insert", new_action) # Test committee dictionary self.test_committee_dict = { "id": "testcommittee1", "title": "testcommittee1", "description": "Test Description", "location": "Test Location", "meeting_time": "1300", "meeting_day": 2, "head": "testuser", "committee_img": "R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" } # Test action dict self.test_action_dict = { "token": self.user_token, "charge": 10, "assigned_to": "testuser", "title": "test title", "description": "test description" }
def setup_method(self, method): db.drop_all() db.create_all() # Create admin user for tests. admin = Users(id="adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal member for tests. self.normal_member = Users(id="testuser") self.normal_member.first_name = "Test1" self.normal_member.last_name = "User" self.normal_member.email = "*****@*****.**" self.normal_member.is_admin = False db.session.add(self.normal_member) db.session.commit() self.normal_member_token = self.normal_member.generate_auth() self.normal_member_token = self.normal_member_token.decode('ascii') # Create normal minute taker for tests. self.minute_taker = Users(id="test2user") self.minute_taker.first_name = "Test2" self.minute_taker.last_name = "User" self.minute_taker.email = "*****@*****.**" self.minute_taker.is_admin = False db.session.add(self.minute_taker) db.session.commit() self.minute_taker_token = self.minute_taker.generate_auth() self.minute_taker_token = self.minute_taker_token.decode('ascii') # Create normal minute taker for tests. self.not_member = Users(id="test3user") self.not_member.first_name = "Test3" self.not_member.last_name = "User" self.not_member.email = "*****@*****.**" self.not_member.is_admin = False db.session.add(self.not_member) db.session.commit() self.not_member_token = self.not_member.generate_auth() self.not_member_token = self.not_member_token.decode('ascii') # Create a test committee. self.committee = Committees(id="testcommittee") self.committee.title = "Test Committee" self.committee.description = "Test Description" self.committee.location = "Test Location" self.committee.meeting_time = "1200" self.committee.meeting_day = 2 self.committee.head = "adminuser" # Add user to committee as normal member. normal_role = Members(role=Roles.NormalMember) normal_role.member = self.normal_member self.committee.members.append(normal_role) minute_taker_role = Members(role=Roles.MinuteTaker) minute_taker_role.member = self.minute_taker self.committee.members.append(minute_taker_role) db.session.add(self.committee) self.user_data = { "token": self.admin_token, "committee_id": self.committee.id, "title": "New Minute", "body": "This is a test body.", "date": 565745465, "private": False } # Create a test charge. charge = Charges(id=10) charge.author = "testuser" charge.title = "Test Charge" charge.description = "Test Description" charge.committee = "testcommittee" charge.paw_links = "https://testlink.com" charge.priority = 0 charge.status = 0 self.charge = charge db.session.add(charge) # Create a new test charge. new_charge = Charges(id=11) new_charge.author = "testuser" new_charge.title = "Test Charge" new_charge.description = "Test Description" new_charge.committee = "testcommittee" new_charge.paw_links = "https://testlink.com" new_charge.priority = 0 new_charge.status = 0 self.new_charge = new_charge db.session.add(new_charge) self.minute = Minutes(title="Test Minute", body="TestBody", date=282827, private=True) self.minute.charges.append(self.charge) self.committee.minutes.append(self.minute) self.public_minute = Minutes(title="Public Test Minute", body="PublicTestBody", date=282827, private=False) self.public_minute.charges.append(self.charge) self.committee.minutes.append(self.public_minute) db.session.commit()
class TestMinutes(object): @classmethod def setup_class(self): self.app = app.test_client() app.config['TESTING'] = True app.config[ 'SQLALCHEMY_DATABASE_URI'] = config.SQLALCHEMY_TEST_DATABASE_URI db = SQLAlchemy(app) db.session.close() db.drop_all() db.create_all() db.event.remove(Committees, "after_insert", new_committee) self.socketio = socketio.test_client(app) self.socketio.connect() @classmethod def setup_method(self, method): db.drop_all() db.create_all() # Create admin user for tests. admin = Users(id="adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal member for tests. self.normal_member = Users(id="testuser") self.normal_member.first_name = "Test1" self.normal_member.last_name = "User" self.normal_member.email = "*****@*****.**" self.normal_member.is_admin = False db.session.add(self.normal_member) db.session.commit() self.normal_member_token = self.normal_member.generate_auth() self.normal_member_token = self.normal_member_token.decode('ascii') # Create normal minute taker for tests. self.minute_taker = Users(id="test2user") self.minute_taker.first_name = "Test2" self.minute_taker.last_name = "User" self.minute_taker.email = "*****@*****.**" self.minute_taker.is_admin = False db.session.add(self.minute_taker) db.session.commit() self.minute_taker_token = self.minute_taker.generate_auth() self.minute_taker_token = self.minute_taker_token.decode('ascii') # Create normal minute taker for tests. self.not_member = Users(id="test3user") self.not_member.first_name = "Test3" self.not_member.last_name = "User" self.not_member.email = "*****@*****.**" self.not_member.is_admin = False db.session.add(self.not_member) db.session.commit() self.not_member_token = self.not_member.generate_auth() self.not_member_token = self.not_member_token.decode('ascii') # Create a test committee. self.committee = Committees(id="testcommittee") self.committee.title = "Test Committee" self.committee.description = "Test Description" self.committee.location = "Test Location" self.committee.meeting_time = "1200" self.committee.meeting_day = 2 self.committee.head = "adminuser" # Add user to committee as normal member. normal_role = Members(role=Roles.NormalMember) normal_role.member = self.normal_member self.committee.members.append(normal_role) minute_taker_role = Members(role=Roles.MinuteTaker) minute_taker_role.member = self.minute_taker self.committee.members.append(minute_taker_role) db.session.add(self.committee) self.user_data = { "token": self.admin_token, "committee_id": self.committee.id, "title": "New Minute", "body": "This is a test body.", "date": 565745465, "private": False } # Create a test charge. charge = Charges(id=10) charge.author = "testuser" charge.title = "Test Charge" charge.description = "Test Description" charge.committee = "testcommittee" charge.paw_links = "https://testlink.com" charge.priority = 0 charge.status = 0 self.charge = charge db.session.add(charge) # Create a new test charge. new_charge = Charges(id=11) new_charge.author = "testuser" new_charge.title = "Test Charge" new_charge.description = "Test Description" new_charge.committee = "testcommittee" new_charge.paw_links = "https://testlink.com" new_charge.priority = 0 new_charge.status = 0 self.new_charge = new_charge db.session.add(new_charge) self.minute = Minutes(title="Test Minute", body="TestBody", date=282827, private=True) self.minute.charges.append(self.charge) self.committee.minutes.append(self.minute) self.public_minute = Minutes(title="Public Test Minute", body="PublicTestBody", date=282827, private=False) self.public_minute.charges.append(self.charge) self.committee.minutes.append(self.public_minute) db.session.commit() @classmethod def teardown_class(self): db.session.close() db.drop_all() db.event.listen(Committees, "after_insert", new_committee) self.socketio.disconnect() def test_get_minutes_no_user(self): self.user_data["token"] = "" self.socketio.emit("get_minutes", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] result = [{ 'id': 2, 'title': 'Public Test Minute', 'body': 'PublicTestBody', 'date': 282827, 'private': False, 'committee_id': 'testcommittee', 'charges': [{ 'id': 10, 'title': "Test Charge" }] }] assert response == result def test_get_minutes_no_committee(self): self.user_data["committee_id"] = "" self.socketio.emit("get_minutes", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.CommitteeDoesntExist def test_get_minutes_not_member(self): self.user_data["token"] = self.not_member_token self.socketio.emit("get_minutes", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] result = [{ 'id': 2, 'title': 'Public Test Minute', 'body': 'PublicTestBody', 'date': 282827, 'private': False, 'committee_id': 'testcommittee', 'charges': [{ 'id': 10, 'title': "Test Charge" }] }] assert response == result def test_get_minutes_success(self): self.socketio.emit("get_minutes", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] result = [{ 'id': 1, 'title': 'Test Minute', 'body': 'TestBody', 'date': 282827, 'private': True, 'committee_id': 'testcommittee', 'charges': [{ 'id': 10, 'title': "Test Charge" }] }, { 'id': 2, 'title': 'Public Test Minute', 'body': 'PublicTestBody', 'date': 282827, 'private': False, 'committee_id': 'testcommittee', 'charges': [{ 'id': 10, 'title': "Test Charge" }] }] assert response == result def test_get_minute_success(self): user_data = { "token": self.normal_member_token, "minute_id": self.minute.id } result = { 'id': 1, 'title': 'Test Minute', 'body': 'TestBody', 'date': 282827, 'private': True, 'committee_id': 'testcommittee', 'charges': [{ 'id': 10, 'title': "Test Charge" }] } self.socketio.emit('get_minute', user_data) received = self.socketio.get_received() assert received[0]["args"][0] == result def test_get_minute_doesnt_exist(self): user_data = {"token": self.normal_member_token, "minute_id": -1} self.socketio.emit('get_minute', user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.MinuteDoesntExist def test_get_private_minute_no_user(self): user_data = {"token": '', "minute_id": self.minute.id} self.socketio.emit('get_minute', user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.PermError def test_get_minute_no_minute(self): self.socketio.emit('get_minute', self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.MinuteDoesntExist def test_get_minute_notmember_private(self): user_data = { "token": self.not_member_token, "minute_id": self.minute.id, "private": True, 'committee_id': '' } self.socketio.emit('get_minute', user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.PermError def test_create_minute_no_user(self): del self.user_data['token'] self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.UserDoesntExist def test_create_minute_no_committee(self): del self.user_data['committee_id'] self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.CommitteeDoesntExist def test_create_minute_no_title(self): del self.user_data['title'] self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteError def test_create_minute_no_date(self): del self.user_data['date'] self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteError def test_create_minute_normal_user_public(self): self.user_data["token"] = self.normal_member_token self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.PermError def test_create_minute_normal_member_private(self): self.user_data["private"] = True self.user_data["token"] = self.normal_member_token self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteSuccess def test_create_minute_minute_taker_public(self): self.user_data["token"] = self.minute_taker_token self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.PermError # Admin create public minute def test_create_minute_admin_public(self): self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteSuccess def test_create_minute_minute_taker_private(self): self.user_data["private"] = True self.user_data["token"] = self.minute_taker_token self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteSuccess def test_create_minute_with_charges(self): self.user_data["charges"] = [self.new_charge.id] self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteSuccess @patch('app.minutes.controllers.db.session.add') def test_create_minute_exception(self, mock_obj): mock_obj.side_effect = Exception("Minute couldn't be added.") self.user_data["token"] = self.admin_token self.user_data["committee_id"] = self.committee.id self.socketio.emit("create_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.AddMinuteError def test_delete_minute_no_user(self): del self.user_data['token'] self.user_data["minute_id"] = self.minute.id self.socketio.emit("delete_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.UserDoesntExist def test_delete_minute_no_minute(self): self.socketio.emit("delete_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.MinuteDoesntExist def test_delete_minute_no_perm(self): self.user_data["token"] = self.normal_member_token self.user_data["minute_id"] = self.minute.id self.socketio.emit("delete_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.PermError def test_delete_minute_success(self): self.user_data["minute_id"] = self.minute.id self.socketio.emit("delete_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.DeleteMinuteSuccess @patch('app.minutes.controllers.db.session.delete') def test_delete_minute_exception(self, mock_obj): mock_obj.side_effect = Exception("Minute couldn't be deleted.") self.user_data["minute_id"] = self.minute.id self.socketio.emit("delete_minute", self.user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.DeleteMinuteError def test_edit_minute_admin(self): user_data = { "token": self.admin_token, "minute_id": self.minute.id, "title": "newtitle", "body": "newbody", "charges": [11] # Get rid of old charge and add new. } self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.EditSuccess def test_edit_minute_committee_head(self): user_data = {"token": self.admin_token, "minute_id": self.minute.id} self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.EditSuccess def test_edit_minute_minute_taker(self): user_data = { "token": self.minute_taker_token, "minute_id": self.minute.id, "private": True } self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.EditSuccess def test_edit_minute_no_user(self): user_data = {"token": "", "minute_id": self.minute.id} self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.UserDoesntExist def test_edit_minute_doesnt_exist(self): user_data = {"token": self.admin_token, "minute_id": -1} self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.MinuteDoesntExist def test_edit_minute_private_not_member(self): user_data = { "token": self.not_member_token, "minute_id": self.minute.id, "private": True, 'committee_id': '' } self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.PermError def test_edit_minute_private_minute_taker(self): user_data = { "token": self.minute_taker_token, "minute_id": self.minute.id, "private": False, 'committee_id': '' } self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.PermError def test_edit_minute_body_success(self): user_data = { "token": self.admin_token, "minute_id": self.minute.id, "body": "test_body", } self.socketio.emit("edit_minute", user_data) received = self.socketio.get_received() response = received[0]["args"][0] assert response == Response.EditSuccess
def setup_method(self, method): db.drop_all() db.create_all() # Create admin user for tests. admin = Users(id = "adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) db.session.commit() self.test_admin = admin self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. user = Users(id = "testuser") user.first_name = "Test1" user.last_name = "User" user.email = "*****@*****.**" user.is_admin = False db.session.add(user) db.session.commit() self.test_user = user self.user_token = user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create normal user for tests. user2 = Users(id = "testuser2") user2.first_name = "Test2" user2.last_name = "User2" user2.email = "*****@*****.**" user2.is_admin = False db.session.add(user2) db.session.commit() self.test_user2 = user2 self.user_token2 = user2.generate_auth() self.user_token2 = self.user_token2.decode('ascii') self.test_committee_dict = { "id" : "testcommittee", "title": "testcommittee", "description": "Test Description", "location": "Test Location", "meeting_time": "1300", "meeting_day": 2, "head": "testuser", "committee_img": "R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" } self.test_committee = Committees(id = "testcommittee") self.test_committee.title = self.test_committee_dict["title"] self.test_committee.location = self.test_committee_dict["location"] self.test_committee.description = self.test_committee_dict["description"] self.test_committee.meeting_time = self.test_committee_dict["meeting_time"] self.test_committee.meeting_day = self.test_committee_dict["meeting_day"] self.test_committee.head = self.test_committee_dict["head"] com_img = base64.b64decode(self.test_committee_dict["committee_img"]) self.test_committee.com_image = com_img db.session.add(self.test_committee) db.session.commit() # Create a test charge. charge = Charges(id = 10) charge.author = "testuser" charge.title = "Test Charge" charge.description = "Test Description" charge.committee = "testcommittee" charge.priority = 0 charge.status = 0 self.test_charge = charge db.session.add(self.test_charge) db.session.commit() # Create a test action. action = Actions(id = 10) action.author = self.test_admin.id action.assigned_to = self.test_user.id action.title = "Test Action" action.description = "Test Description" action.charge = 10 action.status = 0 self.test_action = action db.session.add(self.test_action) db.session.commit()
def setup_method(self, method): db.drop_all() db.create_all() self.user_data = { "user_id": "testuser", "committee_id": "testcommittee", "role": "NormalMember" } # Create admin user for tests. admin = Users(id = "adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) #db.session.expunge(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. self.user = Users(id = "testuser") self.user.first_name = "Test1" self.user.last_name = "User" self.user.email = "*****@*****.**" self.user.is_admin = False db.session.add(self.user) db.session.commit() self.user_token = self.user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create normal user2 for tests. self.user2 = Users(id = "test2user") self.user2.first_name = "Test2" self.user2.last_name = "User" self.user2.email = "*****@*****.**" self.user2.is_admin = False db.session.add(self.user2) db.session.commit() self.user2_token = self.user2.generate_auth() self.user2_token = self.user2_token.decode('ascii') # Create a test committee. self.committee = Committees(id = "testcommittee") self.committee.title = "Test Committee" self.committee.description = "Test Description" self.committee.location = "Test Location" self.committee.meeting_time = "1200" self.committee.meeting_day = 2 self.committee.head = "adminuser" # Create a seconnd test committee. self.committee2 = Committees(id = "testcommittee2") self.committee2.title = "Test Committee" self.committee2.description = "Test Description" self.committee2.location = "Test Location" self.committee2.meeting_time = "1200" self.committee2.meeting_day = 2 self.committee2.head = "adminuser" # Add user2 to committee. role = Members(role= Roles.NormalMember) role.member = self.user2 self.committee.members.append(role) db.session.add(self.committee) db.session.add(self.committee2) db.session.commit()
class TestMembers(object): @classmethod def setup_class(self): self.app = app.test_client() app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = config.SQLALCHEMY_TEST_DATABASE_URI db = SQLAlchemy(app) db.session.close() db.drop_all() db.create_all() db.event.remove(Committees, "after_insert", new_committee) self.socketio = socketio.test_client(app); self.socketio.connect() @classmethod def setup_method(self, method): db.drop_all() db.create_all() self.user_data = { "user_id": "testuser", "committee_id": "testcommittee", "role": "NormalMember" } # Create admin user for tests. admin = Users(id = "adminuser") admin.first_name = "Admin" admin.last_name = "User" admin.email = "*****@*****.**" admin.is_admin = True db.session.add(admin) #db.session.expunge(admin) db.session.commit() self.admin_token = admin.generate_auth() self.admin_token = self.admin_token.decode('ascii') # Create normal user for tests. self.user = Users(id = "testuser") self.user.first_name = "Test1" self.user.last_name = "User" self.user.email = "*****@*****.**" self.user.is_admin = False db.session.add(self.user) db.session.commit() self.user_token = self.user.generate_auth() self.user_token = self.user_token.decode('ascii') # Create normal user2 for tests. self.user2 = Users(id = "test2user") self.user2.first_name = "Test2" self.user2.last_name = "User" self.user2.email = "*****@*****.**" self.user2.is_admin = False db.session.add(self.user2) db.session.commit() self.user2_token = self.user2.generate_auth() self.user2_token = self.user2_token.decode('ascii') # Create a test committee. self.committee = Committees(id = "testcommittee") self.committee.title = "Test Committee" self.committee.description = "Test Description" self.committee.location = "Test Location" self.committee.meeting_time = "1200" self.committee.meeting_day = 2 self.committee.head = "adminuser" # Create a seconnd test committee. self.committee2 = Committees(id = "testcommittee2") self.committee2.title = "Test Committee" self.committee2.description = "Test Description" self.committee2.location = "Test Location" self.committee2.meeting_time = "1200" self.committee2.meeting_day = 2 self.committee2.head = "adminuser" # Add user2 to committee. role = Members(role= Roles.NormalMember) role.member = self.user2 self.committee.members.append(role) db.session.add(self.committee) db.session.add(self.committee2) db.session.commit() @classmethod def teardown_class(self): db.session.close() db.drop_all() db.event.listen(Committees, "after_insert", new_committee) self.socketio.disconnect() # Test get members of nonexistent committee. def test_get_committee_members_nonexistent(self): self.socketio.emit("get_members", "nonexistent") received = self.socketio.get_received() assert received[0]["args"][0] == Response.ComDoesntExist # Test get members of committee. def test_get_committee_members(self): self.socketio.emit("get_members", "testcommittee") received = self.socketio.get_received() commitee = received[0]["args"][0] result = { 'id': 'test2user', 'name': "Test2 User", 'role': 'NormalMember' } assert commitee["committee_id"] == "testcommittee" assert (commitee["members"] == [result]) # Test add to committee when admin. def test_add_to_committee(self): self.user_data["token"] = self.admin_token self.socketio.emit("add_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0]["members"][1]["id"] == self.user_data["user_id"] assert received[1]["args"][0] == Response.AddSuccess # Test add user to more than one committee. def test_add_to_second_committee(self): self.user_data["token"] = self.admin_token self.user_data["committee_id"] = "testcommittee2" self.user_data["user_id"] = "test2user" self.socketio.emit("add_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0]["members"][0]["id"] == self.user_data["user_id"] assert received[1]["args"][0] == Response.AddSuccess # Test add to committee when admin and # no role specified. def test_add_to_committee_no_role(self): self.user_data["token"] = self.admin_token del self.user_data["role"] self.socketio.emit("add_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0]["members"][1]["id"] == self.user_data["user_id"] assert received[0]["args"][0]["members"][1]["role"] == Roles.NormalMember.value assert received[1]["args"][0] == Response.AddSuccess # Test add committee doesnt exist. def test_add_non_existent(self): self.user_data["token"] = self.admin_token self.user_data["committee_id"] = "nonexistent" self.socketio.emit("add_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.UserDoesntExist # Test adding a member raises an Exception. @patch('app.committees.models.Committees.members') def test_add_exception(self, mock_obj): mock_obj.append.side_effect = Exception("User couldn't be added.") self.user_data["token"] = self.admin_token self.socketio.emit("add_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.AddError # Test trying to remove not admin. def test_remove_member_notadmin(self): self.user_data["token"] = self.user_token self.socketio.emit("remove_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.PermError # Test remove member admin def test_remove_member_admin(self): self.user_data["token"] = self.admin_token self.user_data["user_id"] = self.user2.id self.socketio.emit("remove_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0]["members"] == [] assert received[1]["args"][0] == Response.RemoveSuccess # Test remove committee head should fail. def test_remove_head_admin(self): self.user_data["token"] = self.admin_token self.user_data["user_id"] = "adminuser" self.socketio.emit("remove_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.RemoveHeadError # Test remove nonexistent member. def test_remove_member_nonexistent(self): self.user_data["token"] = self.admin_token self.user_data["user_id"] = "nonexistent" self.socketio.emit("remove_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.UserDoesntExist # Test remove member nonexistent committee. def test_remove_member_noncomm(self): self.user_data["token"] = self.admin_token self.user_data["committee_id"] = "nonexistent" self.socketio.emit("remove_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.UserDoesntExist # Test removing a member raises an Exception. @patch('app.members.controllers.db.session.delete') def test_remove_exception(self, mock_obj): mock_obj.side_effect = Exception("User couldn't be removed.") self.user_data["token"] = self.admin_token self.user_data["user_id"] = self.user2.id self.socketio.emit("remove_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.RemoveError # Test trying to remove not admin. def test_edit_member_notadmin(self): self.user_data["token"] = self.user_token self.user_data["role"] = Roles.ActiveMember.value self.socketio.emit("edit_role_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.PermError # Test remove member not admin def test_edit_member_admin(self): self.user_data["token"] = self.admin_token self.user_data["user_id"] = self.user2.id self.user_data["role"] = Roles.ActiveMember.value self.socketio.emit("edit_role_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.EditSuccess # Test remove nonexistent member. def test_edit_member_nonexistent(self): self.user_data["token"] = self.admin_token self.user_data["user_id"] = "nonexistent" self.user_data["role"] = Roles.ActiveMember.value self.socketio.emit("edit_role_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.UserDoesntExist # Test remove member nonexistent committee. def test_edit_member_noncomm(self): self.user_data["token"] = self.admin_token self.user_data["committee_id"] = "nonexistent" self.user_data["role"] = Roles.ActiveMember.value self.socketio.emit("edit_role_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.UserDoesntExist # Test remove member nonexistent role. def test_edit_member_nonrole(self): self.user_data["token"] = self.admin_token self.user_data["role"] = "nonexistent" self.socketio.emit("edit_role_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.RoleDoesntExist # Test editing a member raises an Exception. @patch('app.committees.models.Committees.members') def test_edit_exception(self, mock_obj): mock_obj.filter_by.side_effect = Exception("User couldn't be edited.") self.user_data["token"] = self.admin_token self.user_data["user_id"] = self.user2.id self.user_data["role"] = Roles.ActiveMember.value self.socketio.emit("edit_role_member_committee", self.user_data) received = self.socketio.get_received() assert received[0]["args"][0] == Response.EditError