def test_token_failed_post(self): FNID = "foo42" mock_get = mock.Mock(return_value=mock.Mock( content='<input name="fnid" value="%s">foobar</input>' % FNID)) mock_post = mock.Mock(return_value=mock.Mock(cookies={})) with mock.patch.object(auth.requests, "get", mock_get) as get: with mock.patch.object(auth.requests, "post", mock_post) as post: with self.assertRaisesRegexp(auth.ClientError, ".*Bad user/password.*") as exc: auth.get_token("bad_user", "bad_pass")
def get_token(): """Login on HN and return a user token :user: username string :password: password string Returns a user `token` string which needs to be given as a parameter for API methods which require a user to be signed in such as commenting or voting. Note: your user/password are not stored on the server, but they are transmitted through it. """ try: token = auth.get_token(request.form['user'], request.form['password']) except KeyError: abort(401) except exceptions.ClientError as e: resp = jsonify(error=e.message) resp.status_code = 403 return resp except exceptions.ServerError as e: resp = jsonify(error=e.message) resp.status_code = 500 return resp return jsonify(token=token)
def test_token(self): FNID = "foo42" mock_get = mock.Mock(return_value=mock.Mock( content='<input name="fnid" value="%s">foobar</input>' % FNID)) mock_post = mock.Mock(return_value=mock.Mock( cookies={'user': '******'})) with mock.patch.object(auth.requests, "get", mock_get) as get: with mock.patch.object(auth.requests, "post", mock_post) as post: tok = auth.get_token("test_user", "test_pass") get.assert_called_with(config.HN_LOGIN) post.assert_called_with(config.HN_LOGIN_POST, data={'u': "test_user", 'p': "test_pass", 'fnid': "foo42"}) self.assertEqual(tok, "user_token")
def test_token(self): FNID = "foo42" mock_get = mock.Mock(return_value=mock.Mock( content='<input name="fnid" value="%s">foobar</input>' % FNID)) mock_post = mock.Mock(return_value=mock.Mock( cookies={'user': '******'})) with mock.patch.object(auth.requests, "get", mock_get) as get: with mock.patch.object(auth.requests, "post", mock_post) as post: tok = auth.get_token("test_user", "test_pass") get.assert_called_with(config.HN_LOGIN) post.assert_called_with(config.HN_LOGIN_POST, data={ 'u': "test_user", 'p': "test_pass", 'fnid': "foo42" }) self.assertEqual(tok, "user_token")