Beispiel #1
0
 def test_author_uid_not_tag_uri(self):
     self.activities[0]['object']['author']['id'] = 'not a tag uri'
     resp = self.check_response('/post/fake/%s/000?format=json',
                                expected_status=200)
     props = json_loads(resp.text)['properties']['author'][0]['properties']
     self.assert_equals(['not a tag uri'], props['uid'])
     self.assertNotIn('url', props)
Beispiel #2
0
  def test_post_json(self):
    resp = app.application.get_response(
      '/post/fake/%s/000?format=json' % self.source.key.string_id(), scheme='https')
    self.assertEqual(200, resp.status_int, resp.text)
    self.assert_equals({
      'type': ['h-entry'],
      'properties': {
        'uid': ['tag:fa.ke,2013:000'],
        'url': ['http://fa.ke/000', 'http://or.ig/post'],
        'content': [{ 'html': """\
asdf http://other/link qwert
<a class="u-mention" aria-hidden="true" href="http://other/link"></a>
""",
                      'value': 'asdf http://other/link qwert',
        }],
        'author': [{
            'type': ['h-card'],
            'properties': {
              'uid': [self.source.user_tag_id()],
              'url': ['http://fa.ke/%s' % self.source.key.id()],
              'photo': ['https://example.com/ryan/image'],
            },
        }],
        'category': [{
          'type': ['h-card'],
          'properties': {
            'uid': [self.source.user_tag_id()],
            'url': ['http://or.ig', 'https://fa.ke'],
          },
        }],
      },
    }, json_loads(resp.text))
Beispiel #3
0
    def run(self):
        """The main body of our thread.

        The loop here keeps going until we're killed by the main app.
        When the main app kills us (with join()), socket.listen throws socket.error.
        """
        self.running = True
        while self.running:
            try:
                self.sock.listen(5)
                conn, addr = self.sock.accept()
                self.logger.debug("Got connection from %s", addr[0])
                serialized = bytearray()
                while 1:
                    data = conn.recv(1024)
                    if not data:
                        break
                    serialized += data
                conn.close()
                self.logger.debug("Finished receiving from %s", addr[0])
                try:
                    # first byte is the size of the MAC
                    mac_size = serialized[0]
                    # then the MAC
                    their_digest = serialized[1:mac_size + 1]
                    # then the rest is the serialized data
                    serialized = serialized[mac_size + 1:]
                    mac = hmac.new(self.key, serialized)
                    my_digest = mac.digest()
                except IndexError:  # pragma: no cover
                    raise ValueError('Did not receive any or enough data from %s', addr[0])
                if type(my_digest) is str:
                    self.logger.debug("Computed my digest to be %s; remote is %s", my_digest, their_digest)
                else:
                    self.logger.debug("Computed my digest to be %s; remote is %s", my_digest.hex(), their_digest.hex())
                if not hmac.compare_digest(their_digest, my_digest):
                    raise Exception("Mismatched MAC for network logging data from %s\nMismatched key? Old version of SimpleMonitor?\n" % addr[0])
                try:
                    result = util.json_loads(serialized)
                except JSONDecodeError:
                    result = pickle.loads(serialized)
                try:
                    self.simplemonitor.update_remote_monitor(result, addr[0])
                except Exception:
                    self.logger.exception('Error adding remote monitor')
            except socket.error:
                fail_info = sys.exc_info()
                try:
                    if fail_info[1][0] == 4:
                        # Interrupted system call
                        self.logger.warning("Interrupted system call in thread, I think that's a ^C")
                        self.running = False
                        self.sock.close()
                except IndexError:
                    pass
                if self.running:
                    self.logger.exception("Socket error caught in thread: %s")
            except Exception:
                self.logger.exception("Listener thread caught exception %s")