def test_if_user_created_correctly_from_json(self): user = main.User(single_user[0], {}) with supp_stderr(): check = main.User({}, {}) # need to suppress stderr, because empty object has not id attribute check_attr = (("id", 1), ("name", "Leanne Graham"), ("username", "Bret"), ("email", "*****@*****.**"), ("address", {"street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": {"lat": "-37.3159", "lng": "81.1496"}})) for key, value in check_attr: check.__setattr__(key, value) self.assertEqual(user, check)
def test_check_points(self): pavel = main.User('1') response = self.api.users.get(user_ids=1, fields='city, home_town, sex, bdate, books, ' 'interests, movies, music, personal') pavel2 = pavel.get_info(response[0]) result = main.check_points(pavel, pavel2) self.assertTrue(result > 0)
def begin_work(message): handle = message.text user = main.User(handle) if user.profile is None: bot.send_message(chat_id=message.chat.id, text='Хэндл "{}" не найден'.format(handle)) return if not user.build(): print("Что-то не так") return bot.send_photo(chat_id=message.chat.id, photo=main.draw_pie( user.languages, figure_name="Статистика по языкам программирования", legend_name="Языки программирования"), caption=main.build_str(main.sort_dict(user.languages))) bot.send_photo(chat_id=message.chat.id, photo=main.draw_pie(user.tags, figure_name="Статистика по задачам", legend_name="Тэги"), caption=main.build_str(main.sort_dict(user.tags))) bot.send_photo(chat_id=message.chat.id, photo=main.draw_pie(user.verdicts, figure_name="Статистика по вердиктам", legend_name="Вердикты"), caption=main.build_str(main.sort_dict(user.verdicts))) bot.send_message( chat_id=message.chat.id, text="Всего решалось задач: {}\nВсего решено правильно: {}".format( len(user.attempted), len(user.solved)))
def OnPrivateMessage(self, user, message): #print user.nickname + "!" + user.ident + "@" + user.hostname + ": " + message owner = is_owner(user) if owner and message.lower() == 'turn off': self._bot.go_to_state('Off') return elif owner and message.lower() == 'what state': self._bot.send_message(user.nickname, self._bot.state.name) return elif owner and message.lower() == 'what players': for player in players.iterkeys(): tempuser = players[player] self._bot.send_message( user.nickname, player + ': ' + tempuser.nickname + "!" + tempuser.ident + "@" + tempuser.hostname) elif owner and message.lower() == 'demoderate': self._bot.unmoderate_channel(gamechannel) messagetokens = message.split() if messagetokens[0].lower() == 'replace' and len( messagetokens[0]) == 3: if not nickname_in_game(messagetokens[1].lower()): return olduser = players[messagetokens[1].lower()] newuser = main.User(messagetokens[2], "", "") replace_user(olduser, newuser, self._bot) return
def test_leave_board(): board = main.MessageBoard() board = main.MessageBoard(status="public") user = main.User('Me', main.WhatsAppObserver) board.add_subscriber(user) assert (user in board.event_manager.users) board.remove_subscriber(user) assert (user not in board.event_manager.users)
def test_register_form_username_taken_same_school(self): main.db.session.add(main.User('myusername', 'greatpassword', main.School.query.get(1))) main.db.session.commit() form = main.RegisterForm(username="******", password="******", confirm_password="******", school=main.School.query.get(1)) self.assertFalse(form.validate())
def test_get_card_success(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all(( state_obj, user_obj, main.Card(user_obj, 'q1', 'a1'), main.Card(user_obj, 'q2', 'a2'), )) card_obj_1 = main.Card(user_obj, 'q3', 'a3') card_obj_2 = main.Card(user_obj, 'q4', 'a4') main.db.session.add_all(( card_obj_1, card_obj_2, main.Card(user_obj, 'q5', 'a5') )) state_obj.user = user_obj state_obj._State__current_card_seed = 608731 # [3, 4, 5, 2, 1] main.db.session.commit() print(card_obj_1.id, card_obj_2.id) with self.subTest('get first card'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=1)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_1.id) with self.subTest('get current card'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=0)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_1.id) with self.subTest('get next card'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=1)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_2.id) with self.subTest('get current card (next)'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=0)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_2.id) with self.subTest('overflow iterator'): state_obj._State__current_card_iter = 5 main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=1)) self.assert200(response) self.assertIn(response.json['id'], [i.id for i in main.Card.query.all()])
def setUp(self): main.app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/tmp.sqlite' main.app.config['TESTING'] = True self.app = main.app.test_client() main.db = main.SQLAlchemy(main.app) user = main.User('test') main.db.init_app(main.app) main.db.create_all() main.db.session.commit()
def test_request_advance(name, payscale, requested_advance_amount, expected_output): User_test = main.User(name, payscale) flag = User_test.request_advance(requested_advance_amount) if not (isinstance(expected_output, bool)): print( "\n\n\t\t\tFault in test case. Enter a valid expected output\n\n") else: assert flag == expected_output
def test_answer_no_cards(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.post(flask.url_for('answer_card_api'), data={'a': "a1"}) self.assert400(response)
def test_login_form_success(self): main.db.session.add(main.User(username="******", password="******", school=main.School.query.get(1))) form = main.LoginForm(username="******", password="******", school=main.School.query.get(1)) with self.subTest("validates"): self.assertTrue(form.validate()) with self.subTest("returns user"): self.assertIsInstance(form.user, main.User)
def test_index_logged_in_redirect(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) state_obj.user = user_obj main.db.session.add_all((state_obj, user_obj)) main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('index')) self.assertRedirects(response, flask.url_for('cards'))
def test_play_empty(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('play')) self.assert400(response)
def test_login_form_fail_school(self): main.db.session.add(main.User(username="******", password="******", school=main.School.query.get(1))) form = main.LoginForm(username="******", password="******", school=main.School.query.get(2)) with self.subTest("doesn't validates"): self.assertFalse(form.validate()) with self.subTest("doesn't apply user"): self.assertIsNone(form.user)
def test_remove_card_get_fail(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) card_obj = main.Card(user_obj, 'q1', 'a1') main.db.session.add_all((state_obj, user_obj, card_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('remove_card_api'), data={'id': card_obj.id}) self.assert405(response) self.assertEqual(main.Card.query.filter_by(user=user_obj).count(), 1)
def test_answer_missing_arg(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) card_obj = main.Card(user_obj, 'q1', 'a1') main.db.session.add_all((state_obj, user_obj, card_obj)) state_obj.user = user_obj state_obj._State__current_card_iter = 0 main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.post(flask.url_for('answer_card_api')) self.assert400(response)
def test_add_card_get_fail(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('add_card_api'), data={ "q": "q1", "a": "a1" }) self.assertEqual(response.status_code, 405)
def create_account(username, name, surname, password, repeat_password, tk_root): if password.get() != repeat_password.get(): print("Not matching passwords") return all_users = pickle.load(open('./users.obj', 'rb')) for i, user in enumerate(all_users): if user.username == username.get(): print("Username is taken") return all_users.append( main.User(username.get(), name.get(), surname.get(), password.get())) pickle.dump(all_users, open('./users.obj', 'wb')) print("Account created successfully") tk_root.quit()
def test_answer_card_success(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) card_obj = main.Card(user_obj, 'q1', 'a1') main.db.session.add_all((state_obj, user_obj, card_obj)) state_obj.user = user_obj state_obj._State__current_card_iter = 0 main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.post(flask.url_for('answer_card_api'), data={'a': 'a1'}) self.assert200(response) self.assertTrue(response.json['correct']) self.assertIn('score', response.json)
def home(): # Activity form user = main.User() decision = main.Decision() test_dict = {} user_question_list = utils.json_loader('user_questions.json') decision_question_list = utils.json_loader('decision_questions.json') for idx in range(len(user_question_list)): id_ = user_question_list[idx]['id'] # creates list of questions to populate the page with test_dict[f'question_{id_}'] = user_question_list[idx] for idx in range(len(decision_question_list)): id_ = decision_question_list[idx]['id'] # creates list of questions to populate the page with test_dict[f'question_{id_}'] = decision_question_list[idx] # When submit is clicked if request.method == "POST": # loop through questions and store respective answers for idx in range(len(user_question_list)): id_ = user_question_list[idx]['id'] try: user_question_list[idx]['response'] = request.form[ f'response_{id_}'] except: raise ValueError for idx in range(len(decision_question_list)): id_ = decision_question_list[idx]['id'] try: decision_question_list[idx]['response'] = request.form[ f'response_{id_}'] except: pass user.parse_responses(user_question_list) user.calculate_risk() decision.parse_responses(decision_question_list) decision.evaluate_responses() # Inputted values will be outputted to the screen when submit button is clicked [test] return render_template("home.html", content=test_dict, risk_score=user.risk, feedback=decision.response_list)
def test_get_card_no_cards_fail(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api') + '?n=1') self.assertEqual(response.status_code, 204) with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api') + '?n=0') self.assertEqual(response.status_code, 204)
def test_login_success(self): main.db.session.add(main.User(username="******", password="******", school=main.School.query.get(1))) main.db.session.commit() response = self.client.post(flask.url_for('login'), data={ 'username': "******", 'password': "******", 'school' : '1' }) with self.subTest("redirects"): self.assertRedirects(response, flask.url_for('cards')) with self.subTest("changes state"): state = self.client.cookie_jar._cookies['localhost.local']['/']['game-state'].value state_obj = main.State.query.get(state) self.assertIsNotNone(state_obj) self.assertIsNotNone(state_obj.user)
def test_state_card(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj state_obj._State__current_card_seed = 259720 # [2, 3, 4, 1] main.db.session.add(main.Card(state_obj.user, "card 1", "1")) card_2 = main.Card(state_obj.user, "card 2", "2") main.db.session.add(card_2) main.db.session.add(main.Card(state_obj.user, "card 3", "3")) main.db.session.add(main.Card(state_obj.user, "card 4", "4")) main.db.session.commit() self.assertIs(state_obj.next_card(), card_2) self.assertIsNotNone(state_obj._State__current_card_iter) self.assertIs(state_obj.card, card_2)
def test_require_login_success(self): state_obj = main.State() main.db.session.add(state_obj) user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add(user_obj) state_obj.user = user_obj main.db.session.commit() @main.state_handler @main.require_login def func(state): with self.subTest("test_state_passed"): self.assertIs(state, state_obj) self.assertIsNotNone(state.user) return '', 200 with self.app.test_request_context(headers=Headers([('Cookie', 'game-state={};'.format(state_obj.id))])): response = func() with self.subTest('test_state_passed_through'): self.assertIn("game-state={}".format(state_obj.id), response.headers['set-cookie']) self.assert200(response)
def create_user_list(user_source, post_source): relation = main.make_relation(create_post_list(post_source)) return [main.User(user, relation) for user in user_source]
def test_join_private_board(): board = main.MessageBoard() board = main.MessageBoard(status="private", password="******") user = main.User('Me', main.WhatsAppObserver) board.add_subscriber(user, 'test') assert (user in board.event_manager.users)
def test_fail_join_private_board(): board = main.MessageBoard() board = main.MessageBoard(status="private", password="******") user = main.User('Me', main.WhatsAppObserver) assert (board.add_subscriber(user, "test") == "Wrong Password")
def test_update(): user = main.User('Me', main.WhatsAppObserver) assert (user.observer == main.WhatsAppObserver) user.update(main.EmailObserver) assert (user.observer == main.EmailObserver)
def test_message_received(): board = main.MessageBoard() board = main.MessageBoard(status="public") user = main.User('Me', main.WhatsAppObserver) board.add_subscriber(user) board.add_message("Hello World")
def test_user_check_password_false(self): user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add(user_obj) main.db.session.commit() self.assertFalse(user_obj.check_password("notmypassword"))