#198 Adding unittests
Merged 7 years ago by skrzepto. Opened 7 years ago by skrzepto.

file modified
+96 -21
@@ -1,11 +1,15 @@ 

  import datetime

  import functools

+ import json

  import logging

  import os

+ import uuid

  

  import flask

  import flask.json

+ import fmn.lib

  import munch

+ import pygments.formatters

  import six

  

  from flask.ext.openid import OpenID
@@ -23,17 +27,17 @@ 

  # Register some useful global filters.

  def days_since(then):

      return (datetime.datetime.utcnow() - then).days

+ 

+ 

  app.template_filter('days_since')(days_since)

  app.template_filter('avatar')(username2avatar)

  

  logging.basicConfig()

  

- 

  # TODO - put this in config so we can migrate to pagure

  # TODO - instead of 'develop', use the version from pkg_resources to figure out

  #        the right tag to link people to.  AGPL ftw.

- SOURCE_URL = 'https://pagure.io/fedora-hubs/blob/develop/f'#/hubs/widgets/badges.py'

- 

+ SOURCE_URL = 'https://pagure.io/fedora-hubs/blob/develop/f'  # /hubs/widgets/badges.py'

  

  app.config.from_object('hubs.default_config')

  if 'HUBS_CONFIG' in os.environ:
@@ -41,6 +45,7 @@ 

  

  import fedmsg.config

  import fedmsg.meta

+ 

  fedmsg_config = fedmsg.config.load_config()

  fedmsg.meta.make_processors(**fedmsg_config)

  
@@ -58,9 +63,17 @@ 

              return list(iterable)

          return flask.json.JSONEncoder.default(self, o)

  

+ 

  app.json_encoder = CustomJSONEncoder

  

  

+ def authenticated():

+     """ Utility function checking if the current auth is set or not."""

+     return hasattr(flask.g, 'auth') \

+         and flask.g.auth is not None \

+         and flask.g.auth.logged_in

+ 

+ 

  @app.template_filter('commas')

  def commas(numeric):

      return "{:,.2f}".format(numeric)
@@ -74,7 +87,7 @@ 

  

  @app.route('/')

  def index():

-     if not flask.g.auth.logged_in:

+     if not authenticated():

          return flask.redirect(flask.url_for('login_fedora'))

  

      return flask.redirect(flask.url_for('hub', name=flask.g.auth.nickname))
@@ -82,7 +95,7 @@ 

  

  @app.route('/groups')

  def groups():

-     if not flask.g.auth.logged_in:

+     if not authenticated():

          return flask.redirect(flask.url_for('login_fedora'))

  

      # Get the list of promoted and non-promoted group hubs from the DB
@@ -147,7 +160,7 @@ 

          w.strip().replace('widget-', '')

          for w in flask.request.form.getlist('right_widgets[]')

          if w.strip()

-     ]

+         ]

      try:

          r_widget_ids = [int(w) for w in r_widget_ids]

      except:
@@ -157,7 +170,7 @@ 

      r_indexes = [

          i.strip() for i in flask.request.form.getlist('right_indexes[]')

          if i.strip()

-     ]

+         ]

  

      try:

          r_indexes = [int(i) for i in r_indexes]
@@ -178,7 +191,7 @@ 

          w.strip().replace('widget-', '')

          for w in flask.request.form.getlist('left_widgets[]')

          if w.strip()

-     ]

+         ]

      try:

          l_widget_ids = [int(w) for w in l_widget_ids]

      except:
@@ -188,7 +201,7 @@ 

      l_indexes = [

          i.strip() for i in flask.request.form.getlist('left_indexes[]')

          if i.strip()

-     ]

+         ]

  

      try:

          l_indexes = [int(i) for i in l_indexes]
@@ -281,7 +294,7 @@ 

          widget

          for widget in hubs.widgets.registry

          if hubs.widgets.registry[widget].position in ['both', side]

-     ]

+         ]

      return flask.render_template(

          'add_widget.html',

          hub=hub,
@@ -302,7 +315,7 @@ 

      hub = get_hub(session, name)

  

      widget = widget = hubs.models.Widget(

-         plugin=widget_name, index=-1, left=position=='left')

+         plugin=widget_name, index=-1, left=position == 'left')

      error = False

      config = {}

      for arg in widget.module.data.widget_arguments:
@@ -341,8 +354,8 @@ 

  @app.route('/<hub>/<idx>')

  def widget_render(hub, idx):

      widget = get_widget(session, hub, idx)

-     return widget.render(session)#, edit=False)

-     #was blocking all widgets from working, sorry!

+     return widget.render(session)  # , edit=False)

+     # was blocking all widgets from working, sorry!

  

  

  @app.route('/<hub>/<idx>/json')
@@ -445,7 +458,7 @@ 

  def login():

      default = flask.url_for('index')

      next_url = flask.request.args.get('next', default)

-     if flask.g.auth.logged_in:

+     if authenticated():

          return flask.redirect(next_url)

  

      openid_server = flask.request.form.get('openid', None)
@@ -462,8 +475,8 @@ 

  @app.route('/login/fedora')

  @oid.loginhandler

  def login_fedora():

-     #default = flask.url_for('profile_redirect')

-     #next_url = flask.request.args.get('next', default)

+     # default = flask.url_for('profile_redirect')

+     # next_url = flask.request.args.get('next', default)

      return oid.try_login(

          'https://id.fedoraproject.org',

          ask_for=['email', 'fullname', 'nickname'],
@@ -500,15 +513,17 @@ 

  

  def login_required(function):

      """ Flask decorator to restrict access to logged-in users. """

+ 

      @functools.wraps(function)

      def decorated_function(*args, **kwargs):

          """ Decorated function, actually does the work. """

-         if not flask.g.auth.logged_in:

+         if not authenticated():

              flask.flash('Login required', 'errors')

              return flask.redirect(flask.url_for(

                  'login_fedora', next=flask.request.url))

  

          return function(*args, **kwargs)

+ 

      return decorated_function

  

  
@@ -533,8 +548,8 @@ 

  

  def get_hub(session, name):

      """ Utility shorthand to get a hub and 404 if not found. """

-     hub = session.query(hubs.models.Hub)\

-         .filter(hubs.models.Hub.name == name)\

+     hub = session.query(hubs.models.Hub) \

+         .filter(hubs.models.Hub.name == name) \

          .first()

  

      if not hub:
@@ -559,8 +574,8 @@ 

      flask.abort(404)

  

  

- ## Here are a bunch of API methods that should probably be broken out into

- ## their own file

+ # Here are a bunch of API methods that should probably be broken out into

+ # their own file

  @app.route('/api/hub/<hub>/subscribe', methods=['POST'])

  @login_required

  def hub_subscribe(hub):
@@ -628,3 +643,63 @@ 

          return flask.abort(400)

      session.commit()

      return flask.redirect(flask.url_for('hub', name=hub.name))

+ 

+ 

+ PATHS = fmn.lib.load_rules(root='fmn.rules')

+ 

+ 

+ @app.route('/api/fedmsg/markup', methods=['GET'])

+ def markup_fedmsg():

+     '''

+     This is a temporary endpoint to create a human-readable form of a message.

+     For now it serves as a development tool

+ 

+     This route will be removed once its functionality is integrated into FMN

+     '''

+     from hubs.widgets.feed import (

+         apply_markup, rehydrate_preference,

+         get_remote_preference

+     )

+     try:

+         data = flask.request.args['message']

+         plugin = flask.request.args['plugin']

+     except KeyError:

+         return flask.abort(400)

+     widget = hubs.models.Widget.by_plugin(session, plugin)

+     if not widget:

+         return flask.abort(400)

+     context = widget.config.get('fmn_context')

+     messages = []

+     message = json.loads(data)

+     try:

+         nickname = flask.g.auth.nickname

+     except AttributeError:  # Not logged in

+         return flask.abort(403)

+     preference = get_remote_preference(nickname, context)

+     if preference:

+         try:

+             preference = rehydrate_preference(preference)

+         except ImportError:

+             pass

+     recipients = fmn.lib.recipients(

+         [preference], message, PATHS, fedmsg_config)

+     if recipients:

+         messages.append(message)

+     matches = fedmsg.meta.conglomerate(messages, lexers=True, **fedmsg_config)

+     for match in matches:

+         match['markup'] = apply_markup(match)

+         for _, constituent in match['msg_ids'].items():

+             constituent['markup'] = apply_markup(constituent)

+             if constituent['long_form'] != constituent['subtitle']:

+                 if constituent.get('lexer'):

+                     constituent['long_form'] = pygments.highlight(

+                         constituent['long_form'],

+                         constituent['lexer'],

+                         pygments.formatters.HtmlFormatter(style='monokai'),

+                     )

+                 else:

+                     markup = u"<h5>{long_form}</h5>".format(**constituent)

+                     constituent['long_form'] = markup

+         # And tack on a unique identifier for each top level entry.

+         match['dom_id'] = six.text_type(uuid.uuid4())

+     return flask.jsonify(matches)

file modified
+4
@@ -303,6 +303,10 @@ 

      def by_idx(cls, session, idx):

          return session.query(cls).filter_by(idx=idx).first()

  

+     @classmethod

+     def by_plugin(cls, session, plugin):

+         return session.query(cls).filter_by(plugin=plugin).first()

+ 

      get = by_idx

  

      @property

file modified
+150
@@ -0,0 +1,150 @@ 

+ from contextlib import contextmanager

+ import json

+ from datetime import datetime, timedelta

+ import os

+ 

+ import munch

+ from os.path import dirname

+ import unittest

+ 

+ import requests

+ import vcr

+ 

+ import hubs.models

+ 

+ cassette_dir = dirname(dirname(__file__)) + '/vcr-request-data/'

+ json_path = os.path.join(

+     os.path.join(os.path.dirname(os.path.abspath(__file__))), 'data/')

+ 

+ 

+ class APPTest(unittest.TestCase):

+     def setUp(self):

+         filename = cassette_dir + self.id()

+         self.vcr = vcr.use_cassette(filename, record_mode='new_episodes')

+         self.vcr.__enter__()

+ 

+         import hubs.app

+         hubs.app.fedmsg_config = {

+             'hubs.sqlalchemy.uri': 'sqlite://',  # in memory

If we turn on running the tests on jenkins, we should consider using faitout: http://faitout.fedorainfracloud.org/

This is how I handle it in pagure to keep sqlite/memory DB for local tests and use faitout on jenkins: https://pagure.io/pagure/blob/master/f/tests/__init__.py#_40-53

(This can wait for another PR though)

+         }

+         hubs.app.session = hubs.models.init(

+             hubs.app.fedmsg_config['hubs.sqlalchemy.uri'],

+             create=True,

+         )

+         self.app = hubs.app.app.test_client()

+         self.app.testing = True

+         self.session = hubs.app.session

+         self.populate()

+ 

+     def tearDown(self):

+         self.vcr.__exit__()

+         self.session.remove()

+ 

+     def populate(self):

+         for user in ['devyani7', 'dhrish', 'shalini', 'ralph', 'decause']:

+             openid = '%s.id.fedoraproject.org' % user

+             fullname = user.title()

+             hubs.models.User.get_or_create(

+                 hubs.app.session, openid=openid, fullname=fullname)

+ 

+         hubs.app.session.flush()

+ 

+         hub = hubs.models.Hub.by_name(hubs.app.session, 'ralph')

+         widget = hubs.models.Widget(

+             plugin='about',

+             index=500,

+             _config=json.dumps({"text": "Testing."}),

+         )

+         hub.widgets.append(widget)

+ 

+         for team in ['i18n', 'infra', 'old']:

+             hub = hubs.models.Hub(name=team, summary="the %s team" % team)

+             if hub.name == 'old':

+                 hub.last_refreshed = datetime.utcnow() - timedelta(days=100)

+                 hub.last_updated = datetime.utcnow() - timedelta(days=200)

+ 

+             hubs.app.session.add(hub)

+             widget = hubs.models.Widget(plugin='meetings', index=11,

+                                         _config=json.dumps({'calendar': team}))

+             hub.widgets.append(widget)

+         hubs.app.session.commit()

+ 

+     # TODO - test that it is in the registry

+     # TODO - test that it has all the things it needs

+ 

+     def widget_instance(self, hubname, plugin):

+         hub = hubs.models.Hub.by_name(self.session, hubname)

+         if not hub:

+             raise ValueError("No such hub %r" % hubname)

+         for widget in hub.widgets:

+             if widget.plugin == plugin:

+                 return widget

+         raise KeyError("No such widget found for %s/%s" % (hubname, plugin))

+ 

+     @staticmethod

+     def get_fedmsg(idx):

+         url = 'https://apps.fedoraproject.org/datagrepper/id'

+         response = requests.get(url, params=dict(id=idx))

+         if not bool(response):

+             raise IOError("Failed to talk to %r %r" % (response.url, response))

+         return response.json()

+ 

+ 

+ @contextmanager

+ def auth_set(APP, auth):

+     """

+     Set the provided user as g.auth in the provided application.

+     :param APP: A Flask instance. Cannot be a FlaskClient.

+     :param auth: A FakeAuthorization instance to set as g.auth.

+     """

+ 

+     # Hack used to remove the before_request function set by

+     # flask.ext.fas_openid.FAS which otherwise kills our effort to set a

+     # flask.g.auth.

+     from flask import appcontext_pushed, g

+     APP.before_request_funcs[None] = []

+ 

+     def handler(sender, **kwargs):

+         g.auth = auth

+         if not auth:

+             g.auth = munch.Munch(logged_in=False)

+ 

+     with appcontext_pushed.connected_to(handler, APP):

+         yield

+ 

+ 

+ class FakeUser(object):

+     """ Fake user to be attached to an Authorization. """

+ 

+     def __init__(self, username='username'):

+         """ Constructor.

+         :arg groups: list of the groups in which this fake user is

+             supposed to be.

+         """

+         self.username = username

+         self.openid = username + '.id.fedoraproject.org'

+         self.booksmarks = []

+ 

+     def __getitem__(self, key):

+         return self.dic[key]

+ 

+ 

+ class FakeAuthorization(object):

+     """ Fake Authorization used to set as flask.g.auth. """

+ 

+     def __init__(self, username='username'):

+         """ Constructor.

+         :arg groups: list of the groups in which this fake user is

+             supposed to be.

+         """

+         self.logged_in = True

+         self.fullname = 'fullname: ' + username

+         self.email = 'email: ' + username

+         self.openid = username + '.id.fedoraproject.org'

+         self.user = FakeUser(username)

+         self.avatar = 'avatar_src_url'

+         self.nickname = username

+         self.username = username

+ 

+     def __getitem__(self, key):

+         return self.dic[key]

@@ -0,0 +1,137 @@ 

+ {

+     "timestamp": 1465567413.0,

+     "topic": "io.pagure.prod.pagure.issue.new",

+     "msg_id": "2016-0490127f-473f-4d01-ASDF-a864a56dc302",

+     "msg": {

+         "action": "created",

+         "comment": {

+             "body":

+             "hm, there is something odd the stackage page is inconsistent",

+             "created_at": "2016-06-10T14:03:31Z",

+             "html_url":

+             "https://github.com/fedora-infra/anitya/issues/283#issuecomment-225190438",

+             "id": 225190438,

+             "updated_at": "2016-06-10T14:03:31Z",

+             "url":

+             "https://api.github.com/repos/fedora-infra/anitya/issues/comments/225190438",

+             "user": {

+                 "gravatar_id": "",

+                 "html_url": "https://github.com/pypingou",

+                 "id": 1240038,

+                 "login": "pypingou",

+                 "site_admin": false,

+                 "type": "User",

+                 "url": "https://api.github.com/users/pypingou"

+             }

+         },

+         "fas_usernames": {

+             "fedora-infra": "github_org_fedora-infra",

+             "pypingou": "pingou"

+         },

+         "issue": {

+             "assignee": null,

+             "body":

+             "@juhp it seems that the stackage backend is broken:",

+             "closed_at": "2016-04-29T14:53:10Z",

+             "comments": 8,

+             "created_at": "2016-04-15T16:15:57Z",

+             "html_url":

+             "https://github.com/fedora-infra/anitya/issues/283",

+             "id": 148703615,

+             "labels": [],

+             "locked": false,

+             "milestone": null,

+             "number": 283,

+             "state": "closed",

+             "title": "stackage backend broken",

+             "updated_at": "2016-06-10T14:03:31Z",

+             "url":

+             "https://api.github.com/repos/fedora-infra/anitya/issues/283",

+             "user": {

+                 "gravatar_id": "",

+                 "html_url": "https://github.com/pypingou",

+                 "id": 1240038,

+                 "login": "pypingou",

+                 "site_admin": false,

+                 "type": "User",

+                 "url": "https://api.github.com/users/pypingou"

+             }

+         },

+         "organization": {

+             "description": "Fedora Infrastructure Team",

+             "id": 3316637,

+             "login": "fedora-infra",

+             "url": "https://api.github.com/orgs/fedora-infra"

+         },

+         "repository": {

+             "created_at": "2013-11-29T10:17:26Z",

+             "default_branch": "master",

+             "description": "A cross-distribution upstream release project",

+             "fork": false,

+             "forks": 33,

+             "forks_count": 33,

+             "full_name": "fedora-infra/anitya",

+             "has_downloads": true,

+             "has_issues": true,

+             "has_pages": false,

+             "has_wiki": false,

+             "homepage": "https://release-monitoring.org",

+             "html_url": "https://github.com/fedora-infra/anitya",

+             "id": 14798348,

+             "language": "Python",

+             "name": "anitya",

+             "open_issues": 25,

+             "open_issues_count": 25,

+             "owner": {

+                 "gravatar_id": "",

+                 "html_url": "https://github.com/fedora-infra",

+                 "id": 3316637,

+                 "login": "fedora-infra",

+                 "site_admin": false,

+                 "type": "Organization",

+                 "url": "https://api.github.com/users/fedora-infra"

+             },

+             "private": false,

+             "pushed_at": "2016-06-05T18:16:15Z",

+             "size": 4364,

+             "stargazers_count": 56,

+             "updated_at": "2016-06-10T09:43:58Z",

+             "url": "https://api.github.com/repos/fedora-infra/anitya",

+             "watchers": 56,

+             "watchers_count": 56

+         },

+         "sender": {

+             "gravatar_id": "",

+             "html_url": "https://github.com/pypingou",

+             "id": 1240038,

+             "login": "pypingou",

+             "site_admin": false,

+             "type": "User",

+             "url": "https://api.github.com/users/pypingou"

+         }

+     },

+     "arguments": {

+         "categories": [

+             "pagure"

+         ],

+         "contains": [],

+         "delta": 172800.0,

+         "end": 1465839365.0,

+         "grouped": false,

+         "meta": [],

+         "not_categories": [],

+         "not_packages": [],

+         "not_topics": [],

+         "not_users": [],

+         "order": "desc",

+         "packages": [],

+         "page": 1,

+         "rows_per_page": 1,

+         "start": 1465666565.0,

+         "topics": [],

+         "users": []

+     },

+     "count": 1,

+     "pages": 225,

+     "total": 225

+ }

@@ -0,0 +1,120 @@ 

+ {

+     "topic": "org.fedoraproject.prod.github.issue.comment",

+     "i": 1,

+     "msg": {

+         "action": "created",

+         "comment": {

+             "body":

+             "hm, there is something odd the stackage page is inconsistent",

+             "created_at": "2016-06-10T14:03:31Z",

+             "html_url":

+             "https://github.com/fedora-infra/anitya/issues/283#issuecomment-225190438",

+             "id": 225190438,

+             "updated_at": "2016-06-10T14:03:31Z",

+             "url":

+             "https://api.github.com/repos/fedora-infra/anitya/issues/comments/225190438",

+             "user": {

+                 "gravatar_id": "",

+                 "html_url": "https://github.com/pypingou",

+                 "id": 1240038,

+                 "login": "pypingou",

+                 "site_admin": false,

+                 "type": "User",

+                 "url": "https://api.github.com/users/pypingou"

+             }

+         },

+         "fas_usernames": {

+             "fedora-infra": "github_org_fedora-infra",

+             "pypingou": "pingou"

+         },

+         "issue": {

+             "assignee": null,

+             "body":

+             "@juhp it seems that the stackage backend is broken",

+             "closed_at": "2016-04-29T14:53:10Z",

+             "comments": 8,

+             "created_at": "2016-04-15T16:15:57Z",

+             "html_url":

+             "https://github.com/fedora-infra/anitya/issues/283",

+             "id": 148703615,

+             "labels": [],

+             "locked": false,

+             "milestone": null,

+             "number": 283,

+             "state": "closed",

+             "title": "stackage backend broken",

+             "updated_at": "2016-06-10T14:03:31Z",

+             "url":

+             "https://api.github.com/repos/fedora-infra/anitya/issues/283",

+             "user": {

+                 "gravatar_id": "",

+                 "html_url": "https://github.com/pypingou",

+                 "id": 1240038,

+                 "login": "pypingou",

+                 "site_admin": false,

+                 "type": "User",

+                 "url": "https://api.github.com/users/pypingou"

+             }

+         },

+         "organization": {

+             "description": "Fedora Infrastructure Team",

+             "id": 3316637,

+             "login": "fedora-infra",

+             "url": "https://api.github.com/orgs/fedora-infra"

+         },

+         "repository": {

+             "created_at": "2013-11-29T10:17:26Z",

+             "default_branch": "master",

+             "description":

+             "A cross-distribution upstream release monitoring project",

+             "fork": false,

+             "forks": 33,

+             "forks_count": 33,

+             "full_name": "fedora-infra/anitya",

+             "has_downloads": true,

+             "has_issues": true,

+             "has_pages": false,

+             "has_wiki": false,

+             "homepage": "https://release-monitoring.org",

+             "html_url": "https://github.com/fedora-infra/anitya",

+             "id": 14798348,

+             "language": "Python",

+             "name": "anitya",

+             "open_issues": 25,

+             "open_issues_count": 25,

+             "owner": {

+                 "gravatar_id": "",

+                 "html_url": "https://github.com/fedora-infra",

+                 "id": 3316637,

+                 "login": "fedora-infra",

+                 "site_admin": false,

+                 "type": "Organization",

+                 "url": "https://api.github.com/users/fedora-infra"

+             },

+             "private": false,

+             "pushed_at": "2016-06-05T18:16:15Z",

+             "size": 4364,

+             "stargazers_count": 56,

+             "updated_at": "2016-06-10T09:43:58Z",

+             "url": "https://api.github.com/repos/fedora-infra/anitya",

+             "watchers": 56,

+             "watchers_count": 56

+         },

+         "sender": {

+             "gravatar_id": "",

+             "html_url": "https://github.com/pypingou",

+             "id": 1240038,

+             "login": "pypingou",

+             "site_admin": false,

+             "type": "User",

+             "url": "https://api.github.com/users/pypingou"

+         }

+     },

+     "msg_id": "2016-0490127f-473f-4d01-8e3a-a864a56dc302",

+     "signature":

+     "MNpAVdrPW78dcSe94FOS+/6QFBYo/LkrV5ee9tQedkhurHY1BGpaFHlU5l8cNVEpB7rr",

+     "source_name": "datanommer",

+     "source_version": "0.6.5",

+     "timestamp": 1465567413.0,

+     "topic": "org.fedoraproject.prod.github.issue.comment"

+ }

@@ -0,0 +1,82 @@ 

+ import json

+ import os

+ 

+ import hubs.tests

+ import hubs.models

+ from hubs.app import app

+ from hubs.tests import json_path

+ 

+ 

+ class TestFeed(hubs.tests.APPTest):

+ 

+     with open(json_path + 'message_involved.json', 'r') as fp:

+         message_involved = json.loads(fp.read())

+ 

+     with open(json_path + 'message_not_involved.json', 'r') as fp:

+         message_not_involved = json.loads(fp.read())

+ 

+     # TODO this test relies on a specific filter for the atelic user

+     # It would be better to fake preferences on FAS

We likely do not want to query FAS to run our tests :)

+     user = hubs.tests.FakeAuthorization('atelic')

+ 

+     plugin = 'feed'

+ 

+     def test_returns_400_when_no_message(self):

+         payload = {'plugin': 'feed'}

+         response = self.app.get('/api/fedmsg/markup', query_string=payload)

+         self.assertEqual(response.status_code, 400)

+ 

+     def test_returns_400_when_no_plugin(self):

+         payload = {'message': json.dumps(self.message_involved)}

+         response = self.app.get('/api/fedmsg/markup', query_string=payload)

+         self.assertEqual(response.status_code, 400)

+ 

+     def test_returns_400_when_bad_plugin_name(self):

+         payload = {

+             'message': json.dumps(self.message_involved),

+             'plugin': 'notarealplugin'

+         }

+         response = self.app.get('/api/fedmsg/markup', query_string=payload)

+         self.assertEqual(response.status_code, 400)

+ 

+     def test_returns_403_when_not_logged_in(self):

+         payload = {

+             'message': json.dumps(self.message_involved),

+             'plugin': 'feed'

+         }

+ 

+         response = self.app.get('/api/fedmsg/markup', query_string=payload)

+         self.assertEqual(response.status_code, 403)

+ 

+     def test_returns_match_if_involved(self):

+         payload = {

+             'message': json.dumps(self.message_involved),

+             'plugin': 'feed'

+         }

+         with self.app.session_transaction() as sess:

+             sess['openid'] = 'atelic@fedoraproject.org'

+             sess['nickname'] = 'atelic'

+ 

+         with hubs.tests.auth_set(app, self.user):

+             response = self.app.get('/api/fedmsg/markup', query_string=payload)

+ 

+             self.assertEqual(response.status_code, 200)

+             data = json.loads(response.data)

+             self.assertTrue(data)

+             self.assertTrue(isinstance(data[0], dict))

+ 

+     def test_returns_no_match_if_not_involved(self):

+         payload = {

+             'message': json.dumps(self.message_not_involved),

+             'plugin': 'feed'

+         }

+         with self.app.session_transaction() as sess:

+             sess['openid'] = 'atelic@fedoraproject.org'

+             sess['nickname'] = 'atelic'

+ 

+         with hubs.tests.auth_set(app, self.user):

+             response = self.app.get('/api/fedmsg/markup', query_string=payload)

+ 

+             self.assertEqual(response.status_code, 200)

+             data = json.loads(response.data)

+             self.assertFalse(data)

@@ -0,0 +1,151 @@ 

+ import flask

+ import unittest

+ from urlparse import urlparse

+ 

+ import hubs.tests

+ import hubs.models

+ from hubs.app import app

+ 

+ 

+ def usernames(collection): return [u.username for u in collection]

+ 

+ 

+ class TestHubSubscribe(hubs.tests.APPTest):

+     user = hubs.tests.FakeAuthorization('decause')

+ 

+     def test_subscribe_redirects_when_logged_out(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         resp = self.app.post('/api/hub/{}/subscribe'.format(hub.name),

+                              follow_redirects=False)

+         self.assertEqual(resp.status_code, 302)

+         self.assertEqual(urlparse(resp.location).path, '/login/fedora')

+ 

+     def test_subscribe_when_logged_in(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         with hubs.tests.auth_set(app, self.user):

+             resp = self.app.post('/api/hub/{}/subscribe'.format(hub.name),

+                                  follow_redirects=True)

+             self.assertEqual(resp.status_code, 200)

+             # Need to find the Hub again to avoid DetachedInstanceError

+             h = hubs.models.Hub.by_name(self.session, 'infra')

+             self.assertTrue(self.user.username in usernames(h.subscribers))

+ 

+ 

+ class TestHubUnsubscribe(hubs.tests.APPTest):

+     user = hubs.tests.FakeAuthorization('decause')

+ 

+     def test_unsubscribe_redirects_when_logged_out(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         resp = self.app.post('/api/hub/{}/unsubscribe'.format(hub.name),

+                              follow_redirects=False)

+         self.assertEqual(resp.status_code, 302)

+         self.assertEqual(urlparse(resp.location).path, '/login/fedora')

+ 

+     def test_unsubscribe_when_logged_in(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         # Need a real user model to subscribe to the hub

+         User = hubs.models.User.by_username(self.session, self.user.username)

+         hub.subscribe(self.session, User)

+ 

+         self.assertTrue(self.user.username in usernames(hub.subscribers))

+ 

+         with hubs.tests.auth_set(app, self.user):

+             resp = self.app.post('/api/hub/{}/unsubscribe'.format(hub.name),

+                                  follow_redirects=True)

+             self.assertEqual(resp.status_code, 200)

+             h = hubs.models.Hub.by_name(self.session, 'infra')

+             self.assertTrue(self.user.username not in usernames(h.subscribers))

+ 

+ 

+ class TestHubStar(hubs.tests.APPTest):

+     user = hubs.tests.FakeAuthorization('decause')

+ 

+     def test_star_redirects_when_logged_out(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         resp = self.app.post('/api/hub/{}/star'.format(hub.name),

+                              follow_redirects=False)

+         self.assertEqual(resp.status_code, 302)

+         self.assertEqual(urlparse(resp.location).path, '/login/fedora')

+ 

+     def test_star_when_logged_in(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         with hubs.tests.auth_set(app, self.user):

+             resp = self.app.post('/api/hub/{}/star'.format(hub.name),

+                                  follow_redirects=True)

+ 

+             self.assertEqual(resp.status_code, 200)

+             h = hubs.models.Hub.by_name(self.session, 'infra')

+             self.assertTrue(self.user.username in usernames(h.stargazers))

+ 

+ 

+ class TestHubUnstar(hubs.tests.APPTest):

+     user = hubs.tests.FakeAuthorization('decause')

+ 

+     def test_unstar_redirects_when_logged_out(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         resp = self.app.post('/api/hub/{}/unstar'.format(hub.name),

+                              follow_redirects=False)

+         self.assertEqual(resp.status_code, 302)

+         self.assertEqual(urlparse(resp.location).path, '/login/fedora')

+ 

+     def test_unstar_when_logged_in(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         # Need a real user model to subscribe to the hub

+         User = hubs.models.User.by_username(self.session, self.user.username)

+         hub.subscribe(self.session, User, role='stargazer')

+ 

+         self.assertTrue(self.user.username in [u.username for

+                                                u in hub.stargazers])

+ 

+         with hubs.tests.auth_set(app, self.user):

+             resp = self.app.post('/api/hub/{}/unstar'.format(hub.name),

+                                  follow_redirects=True)

+             self.assertEqual(resp.status_code, 200)

+             h = hubs.models.Hub.by_name(self.session, 'infra')

+             self.assertTrue(self.user.username not in usernames(h.stargazers))

+ 

+ 

+ class TestHubJoin(hubs.tests.APPTest):

+     user = hubs.tests.FakeAuthorization('decause')

+ 

+     def test_join_redirects_when_logged_out(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         resp = self.app.post('/api/hub/{}/join'.format(hub.name),

+                              follow_redirects=False)

+         self.assertEqual(resp.status_code, 302)

+         self.assertEqual(urlparse(resp.location).path, '/login/fedora')

+ 

+     def test_join_when_logged_in(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         with hubs.tests.auth_set(app, self.user):

+             resp = self.app.post('/api/hub/{}/join'.format(hub.name),

+                                  follow_redirects=True)

+ 

+             self.assertEqual(resp.status_code, 200)

+             h = hubs.models.Hub.by_name(self.session, 'infra')

+             self.assertTrue(self.user.username in usernames(h.members))

+ 

+ 

+ class TestHubLeave(hubs.tests.APPTest):

+     user = hubs.tests.FakeAuthorization('decause')

+ 

+     def test_leave_redirects_when_logged_out(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         resp = self.app.post('/api/hub/{}/leave'.format(hub.name),

+                              follow_redirects=False)

+         self.assertEqual(resp.status_code, 302)

+         self.assertEqual(urlparse(resp.location).path, '/login/fedora')

+ 

+     def test_star_when_logged_in(self):

+         hub = hubs.models.Hub.by_name(self.session, 'infra')

+         # Need a real user model to subscribe to the hub

+         User = hubs.models.User.by_username(self.session, self.user.username)

+         hub.subscribe(self.session, User, role='member')

+ 

+         with hubs.tests.auth_set(app, self.user):

+             resp = self.app.post('/api/hub/{}/leave'.format(hub.name),

+                                  follow_redirects=True)

+ 

+             self.assertEqual(resp.status_code, 200)

+             h = hubs.models.Hub.by_name(self.session, 'infra')

+             self.assertTrue(self.user.username not in usernames(h.members))

@@ -0,0 +1,285 @@ 

+ import unittest

+ from urlparse import urlparse

+ 

+ from flask import json

+ from os.path import dirname

+ import vcr

+ from werkzeug.datastructures import ImmutableMultiDict

+ 

+ import hubs

+ from hubs import tests

+ from hubs.app import app

+ 

+ import hubs.models

+ 

+ import fedmsg.config

+ 

+ cassette_dir = dirname(dirname(__file__)) + '/vcr-request-data/'

+ 

+ 

+ class HubsAPITest(hubs.tests.APPTest):

+     def test_index_logged_out(self):

+         result = self.app.get('/', follow_redirects=False)

+         self.assertEqual(result.status_code, 302)

+         self.assertEqual(urlparse(result.location).path, "/login/fedora")

+ 

+     def test_index_logged_in(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/', follow_redirects=True)

+             # its trying to redirect to login id.fedoraproject.org/openid

+             # assert the status code of the response

+             self.assertEqual(result.status_code, 200)

+             self.assertFalse('Not logged in.  Click to <a href="'

+                              '/login/fedora">login</a>' in result.data)

+ 

+     def test_hub_logged_out(self):

+         with app.test_request_context('/ralph'):

+             # need to manually call the @app.before_request

+             # since unittest don't call it

+             hubs.app.check_auth()

+             result = self.app.get('/ralph', follow_redirects=True)

+             # assert the status code of the response

+             self.assertEqual(result.status_code, 200)

+             str_expected = 'Not logged in.  Click to ' \

+                            '<a href="/login/fedora">login</a>'

+             self.assertTrue(str_expected in result.data)

+ 

+     def test_groups_logged_out(self):

+         result = self.app.get('/groups', follow_redirects=False)

+         # assert the status code of the response

+         self.assertEqual(result.status_code, 302)

+         # this will redirect to fedora.login

+         self.assertEqual(urlparse(result.location).path, "/login/fedora")

+ 

+     def test_groups_logged_in(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/groups', follow_redirects=True)

+             # assert the status code of the response

+             self.assertEqual(result.status_code, 200)

+             self.assertTrue("ZOMG -  is the Hub Of The Month!" in result.data)

+ 

+     def test_hub_logged_in(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/ralph', follow_redirects=True)

+             self.assertEqual(result.status_code, 200)

+             self.assertFalse('Not logged in.  Click to <a href="'

+                              '/login/fedora">login</a>' in result.data)

+ 

+     def test_hub_json(self):

+         result = self.app.get('/ralph/json', follow_redirects=True)

+         # assert the status code of the response

+         self.assertEqual(result.status_code, 200)

+         data = {

+             "avatar": "https://seccdn.libravatar.org/avatar/"

+                       "9c9f7784935381befc302fe3c814f9136e7a339"

+                       "53d0318761669b8643f4df55c?s=312&d=retro",

+             "left_width": 8,

+             "members": ["ralph"],

+             "name": "ralph",

+             "owners": ["ralph"],

+             "subscribers": [],

+             "summary": "Ralph",

+             "widgets": [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 51]

+         }

+         self.assertDictEqual(data, json.loads(result.data))

+ 

+     @unittest.skip("Authorization layer not present yet")

+     def test_hub_edit_get_logged_out(self):

+         # FAILING: Authorization layer not present yet

+         with tests.auth_set(app, None):

+             result = self.app.get('/ralph/edit', follow_redirects=True)

+             self.assertEqual(result.status_code, 403)  # failing right

+             # We do not have the authorization layer in place yet

+ 

+     @unittest.skip("Authorization layer not present yet")

+     def test_hub_edit_get_logged_in_not_owner(self):

+         # FAILING: Authorization layer not present yet

+         user = tests.FakeAuthorization('not_ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/ralph/edit', follow_redirects=True)

+             self.assertEqual(result.status_code, 403)  # failing right

+             # We do not have the authorization layer in place yet

+ 

+     def test_hub_edit_get_logged_in_owner(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/ralph/edit', follow_redirects=True)

+             self.assertEqual(result.status_code, 200)

+             # We do not have the authorization layer so everyone can edit

+ 

+     @unittest.skip("Authorization layer not present yet")

+     def test_hub_edit_post_logged_out(self):

+         # FAILING: Authorization layer not present yet

+         with tests.auth_set(app, None):

+             result = self.app.post('/ralph/edit', follow_redirects=True)

+             self.assertEqual(result.status_code, 403)  # failing right

+             # We do not have the authorization layer in place yet

+ 

+     @unittest.skip("Authorization layer not present yet")

+     def test_hub_edit_post_logged_in_not_owner(self):

+         # FAILING: Authorization layer not present yet

+         user = tests.FakeAuthorization('not_ralph')

+         with tests.auth_set(app, user):

+             result = self.app.post('/ralph/edit', follow_redirects=True)

+             self.assertEqual(result.status_code, 403)  # failing right

+             # We do not have the authorization layer in place yet

+ 

+     def test_hub_edit_post_logged_in_owner_empty_data(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.post('/ralph/edit', data={},

+                                    follow_redirects=True)

+             self.assertEqual(result.status_code, 200)

+             self.assertFalse('Not logged in.  Click to <a href="'

+                              '/login/fedora">login</a>' in result.data)

+             self.assertTrue('<a href="#" class="dropdown-item">Full Name: '

+                             'fullname: ralph</a>' in result.data)

+ 

+     def test_hub_edit_post_logged_in_owner_valid_data(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             data = ImmutableMultiDict(

+                 [('right_indexes[]', u'0'), ('right_indexes[]', u'1'),

+                  ('right_indexes[]', u'2'), ('right_indexes[]', u'3'),

+                  ('right_indexes[]', u'4'), ('right_indexes[]', u'5'),

+                  ('right_indexes[]', u'6'), ('right_indexes[]', u'7'),

+                  ('right_indexes[]', u'8'), ('right_widgets[]', u'32'),

+                  ('right_widgets[]', u'33'), ('right_widgets[]', u'34'),

+                  ('right_widgets[]', u'35'), ('right_widgets[]', u'36'),

+                  ('right_widgets[]', u'37'), ('right_widgets[]', u'38'),

+                  ('right_widgets[]', u'39'), ('right_widgets[]', u'40'),

+                  ('js', u'true'), ('left_indexes[]', u'0'),

+                  ('left_indexes[]', u'1'), ('left_widgets[]', u'31'),

+                  ('left_widgets[]', u'32')])

+             result = self.app.post('/ralph/edit', data=data,

+                                    follow_redirects=True)

+             self.assertEqual(result.status_code, 200)

+             self.assertEqual(result.data, 'ok')

+ 

+     def test_hub_edit_post_logged_in_owner_invalid_data_1(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             # some indexes and widgets are not integers

+             data = ImmutableMultiDict(

+                 [('right_indexes[]', u'0a'), ('right_indexes[]', u'1'),

+                  ('right_indexes[]', u'2'), ('right_indexes[]', u'3'),

+                  ('right_indexes[]', u'4'), ('right_indexes[]', u'5'),

+                  ('right_indexes[]', u'6'), ('right_indexes[]', u'7'),

+                  ('right_indexes[]', u'8'), ('right_widgets[]', u'32'),

+                  ('right_widgets[]', u'33a'), ('right_widgets[]', u'34'),

+                  ('right_widgets[]', u'35'), ('right_widgets[]', u'36'),

+                  ('right_widgets[]', u'37'), ('right_widgets[]', u'38'),

+                  ('right_widgets[]', u'39'), ('right_widgets[]', u'40'),

+                  ('js', u'true'), ('left_indexes[]', u'0a'),

+                  ('left_indexes[]', u'1'), ('left_widgets[]', u'31a'),

+                  ('left_widgets[]', u'32')])

+             result = self.app.post('/ralph/edit', data=data,

+                                    follow_redirects=True)

+             self.assertEqual(result.status_code, 400)

+ 

+     def test_hub_edit_post_logged_in_owner_invalid_data_2(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             # indexes len don't match widgets len

+             data = ImmutableMultiDict(

+                 [('right_indexes[]', u'1'), ('right_indexes[]', u'2'),

+                  ('right_indexes[]', u'3'), ('right_indexes[]', u'4'),

+                  ('right_indexes[]', u'5'), ('right_indexes[]', u'6'),

+                  ('right_indexes[]', u'7'), ('right_indexes[]', u'8'),

+                  ('right_widgets[]', u'32'), ('right_widgets[]', u'34'),

+                  ('right_widgets[]', u'35'), ('right_widgets[]', u'36'),

+                  ('right_widgets[]', u'37'), ('right_widgets[]', u'38'),

+                  ('right_widgets[]', u'39'), ('right_widgets[]', u'40'),

+                  ('js', u'true'), ('left_indexes[]', u'0'),

+                  ('left_indexes[]', u'1'), ('left_widgets[]', u'32')])

+             result = self.app.post('/ralph/edit', data=data,

+                                    follow_redirects=True)

+             self.assertEqual(result.status_code, 400)

+ 

+     def test_login_already_loggedin(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/login', follow_redirects=False)

+             self.assertEqual(result.status_code, 302)

+             self.assertEqual(urlparse(result.location).path, "/")

+ 

+     def test_hub_add_widget_get_no_args(self):

+         result = self.app.get('/ralph/add', follow_redirects=False)

+         self.assertEqual(result.status_code, 400)

+         expected_str = 'Invalid position provided'

+         self.assertTrue(expected_str in result.data)

+ 

+     def test_hub_add_widget_get_with_args(self):

+         result = self.app.get('/ralph/add?position=right',

+                               follow_redirects=True)

+         self.assertEqual(result.status_code, 200)

+         expected_str = 'Adding widget to hub: ralph'

+         self.assertTrue(expected_str in result.data)

+         expected_str = "url: 'add/' + $('#widget').val() + '?position=right',"

+         self.assertTrue(expected_str in result.data)

+ 

+     def test_hub_add_widget_post_no_widget_name(self):

+         data = {}

+         result = self.app.post('/ralph/add', data=data, follow_redirects=False)

+         self.assertEqual(result.status_code, 400)

+         expected_str = 'Invalid request sent'

+         self.assertTrue(expected_str in result.data)

+ 

+     def test_hub_add_widget_post_invalid_widget_name(self):

+         data = {'widget_name': 'invalid_widget_name'}

+         result = self.app.post('/ralph/add', data=data, follow_redirects=False)

+         self.assertEqual(result.status_code, 404)

+         expected_str = 'Unknown widget called'

+         self.assertTrue(expected_str in result.data)

+ 

+     def test_hub_add_widget_post_valid_widget_name_no_args(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             data = {'widget_name': 'about'}

+             result = self.app.post('/ralph/add', data=data,

+                                    follow_redirects=False)

+             self.assertEqual(result.status_code, 200)

+             expected_str = '<h6 class="dropdown-header">Account Information</h6>'

+             self.assertTrue(expected_str in result.data)

+             expected_str = '<a href="#" class="dropdown-item">Full Name: fullname: ralph</a>'

+             self.assertTrue(expected_str in result.data)

+ 

+     def test_hub_add_widget_post_valid_widget_name_with_args(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             data = {'widget_name': 'about', 'text': 'text of widget'}

+             result = self.app.post('/ralph/add', data=data,

+                                    follow_redirects=False)

+             self.assertEqual(result.status_code, 200)

+             expected_str = '<h6 class="dropdown-header">Account Information</h6>'

+             self.assertTrue(expected_str in result.data)

+             expected_str = '<a href="#" class="dropdown-item">Full Name: fullname: ralph</a>'

+             self.assertTrue(expected_str in result.data)

+ 

+     def test_hub_edit_widget_get_logged_in(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             result = self.app.get('/ralph/31/edit', follow_redirects=True)

+             self.assertEqual(result.status_code, 200)

+             expected_str = '<input id="fmn_context" class="form-control" type="text"'

+             self.assertTrue(expected_str in result.data)

+ 

+     @unittest.skip("Authorization layer not present yet")

+     def test_hub_edit_widget_get_logged_out(self):

+         result = self.app.get('/ralph/31/edit', follow_redirects=True)

+         self.assertEqual(result.status_code, 403)

+ 

+     def test_hub_edit_widget_post_empty_data_logged_in(self):

+         user = tests.FakeAuthorization('ralph')

+         with tests.auth_set(app, user):

+             data = {}

+             url = '/ralph/31/edit'

+             result = self.app.post(url, data=data, follow_redirects=False)

+             self.assertEqual(result.status_code, 302)

+             self.assertEqual(urlparse(result.location).path, '/ralph/')

+ 

+ if __name__ == '__main__':

+     unittest.main()

@@ -1,5 +1,7 @@ 

  import json

  

+ from nose.tools import assert_dict_equal

+ 

  import hubs.tests.test_widgets

  

  
@@ -17,3 +19,24 @@ 

              u'source_url': u'/source/about',

              u'text': u'Testing.',

          })

+ 

+     def test_should_invalidate_wrong_topic(self):

+         widget = self.widget_instance('ralph', self.plugin)

+         msg = {'topic': 'hubs.widget.update.WRONG.TOPIC', 'msg': {'widget': {'id': widget.idx}}}

+         module = hubs.widgets.registry[widget.plugin]

+         result = module.should_invalidate(msg, self.session, widget)

+         self.assertFalse(result)

+ 

+     def test_should_invalidate_wrong_widget_id(self):

+         widget = self.widget_instance('ralph', self.plugin)

+         msg = {'topic': 'hubs.widget.update', 'msg': {'widget': {'id': widget.idx+1}}}

+         module = hubs.widgets.registry[widget.plugin]

+         result = module.should_invalidate(msg, self.session, widget)

+         self.assertFalse(result)

+ 

+     def test_should_invalidate_good_match(self):

+         widget = self.widget_instance('ralph', self.plugin)

+         msg = {'topic': 'hubs.widget.update', 'msg': {'widget': {'id': widget.idx}}}

+         module = hubs.widgets.registry[widget.plugin]

+         result = module.should_invalidate(msg, self.session, widget)

+         self.assertTrue(result)

@@ -1,5 +1,6 @@ 

  import json

  

+ from mock import patch, MagicMock

  import hubs.tests.test_widgets

  

  
@@ -12,25 +13,36 @@ 

          assert response.status_code == 200, response.status_code

          data = json.loads(response.data)

          self.assertEquals(data['plugin'], 'badges')

-         assert 'assertions' in data['data'], data['data'].keys()

+         self.assertIn('assertions', data['data'].keys())

  

      def test_should_invalidate_wrong_topic(self):

          widget = self.widget_instance('ralph', self.plugin)

-         msg = self.get_fedmsg('2016-ebb84660-59e9-4e68-af8f-4e6f49348b88')

+         # msg = self.get_fedmsg('2016-ebb84660-59e9-4e68-af8f-4e6f49348b88')

+         msg = {'topic': 'hubs.widget.update.WRONG.TOPIC'}

          module = hubs.widgets.registry[widget.plugin]

          result = module.should_invalidate(msg, self.session, widget)

-         assert not result

+         self.assertFalse(result)

  

      def test_should_invalidate_wrong_user(self):

          widget = self.widget_instance('ralph', self.plugin)

-         msg = self.get_fedmsg('2016-e371c7f6-bc8e-4632-8e33-b9102dc30b5f')

+         # msg = self.get_fedmsg('2016-e371c7f6-bc8e-4632-8e33-b9102dc30b5f')

+         msg = {'topic': 'fedbadges.badge.award', 'msg': {'user': {'username': 'not_ralph'}}}

          module = hubs.widgets.registry[widget.plugin]

          result = module.should_invalidate(msg, self.session, widget)

-         assert not result

+         self.assertFalse(result)

  

-     def test_should_invalidate_good_match(self):

+     def test_should_invalidate_good_match_fedbadges(self):

          widget = self.widget_instance('ralph', self.plugin)

-         msg = self.get_fedmsg('2016-1fbb1135-681b-4d3b-9a40-d0f6ebd313f4')

+         # msg = self.get_fedmsg('2016-1fbb1135-681b-4d3b-9a40-d0f6ebd313f4')

+         msg = {'topic': 'fedbadges.badge.award', 'msg': {'user': {'username': 'ralph'}}}

          module = hubs.widgets.registry[widget.plugin]

          result = module.should_invalidate(msg, self.session, widget)

-         assert result

+         self.assertTrue(result)

+ 

+     def test_should_invalidate_good_match_hubswidget(self):

+         widget = self.widget_instance('ralph', self.plugin)

+         # msg = self.get_fedmsg('2016-1fbb1135-681b-4d3b-9a40-d0f6ebd313f4')

+         msg = {'topic': 'hubs.widget.update', 'msg': {'widget': {'id': widget.idx + 1}}}

+         module = hubs.widgets.registry[widget.plugin]

+         result = module.should_invalidate(msg, self.session, widget)

+         self.assertTrue(result) 

\ No newline at end of file

@@ -10,14 +10,14 @@ 

          team = 'i18n'

          widget = self.widget_instance(team, self.plugin)

          response = self.app.get('/%s/%i/json/' % (team, widget.idx))

-         assert response.status_code == 200, response.status_code

+         self.assertEqual(200, response.status_code)

          data = json.loads(response.data)

          meeting = data['data']['meetings']["%s meeting" % team]

-         assert team in meeting['meeting_name']

+         self.assertIn(team, meeting['meeting_name'])

  

      def test_render_simple(self):

          team = 'i18n'

          widget = self.widget_instance(team, self.plugin)

          response = self.app.get('/%s/%i/' % (team, widget.idx))

-         assert response.status_code == 200, response.status_code

-         assert 'The i18n meeting is in' in response.data, response.data

+         self.assertEqual(200, response.status_code)

+         self.assertIn('The i18n meeting is ', response.data)

file modified
+1 -1
@@ -21,6 +21,6 @@ 

  def should_invalidate(message, session, widget):

      if not message['topic'].endswith('hubs.widget.update'):

          return False

-     if message['msg']['widget']['id'] != widget.id:

+     if message['msg']['widget']['id'] != widget.idx:

          return False

      return True

file modified
+1 -1
@@ -30,7 +30,7 @@ 

  @hint(topics=[_('hubs.widget.update'), _('fedbadges.badge.award')])

  def should_invalidate(message, session, widget):

      if message['topic'].endswith('hubs.widget.update'):

-         if message['msg']['widget']['id'] != widget.id:

+         if message['msg']['widget']['id'] != widget.idx:

              return True

  

      if message['topic'].endswith('fedbadges.badge.award'):

Atelic and I have added 48 new tests and thought it would be a good time to merge unittest into master.


currently at 64% coverage.

Could we move this import as well as the from hubs.widgets.feed above down into the endpoint so that we know they should go away when we remove this endpoint as well?

1 new commit added

  • Move hubs.feed and PATHS down to temp endpoint
7 years ago

Is this the equivalent of self.assertNotNone()?

Should check more precisely what is returned? (Can we?)

yea i'll delete that, this was because i was getting flask errors and decided to test the route instead

1 new commit added

  • cleaning up current tests in the widgets folder
7 years ago

Anything else needed on this?

If we turn on running the tests on jenkins, we should consider using faitout: http://faitout.fedorainfracloud.org/

This is how I handle it in pagure to keep sqlite/memory DB for local tests and use faitout on jenkins: https://pagure.io/pagure/blob/master/f/tests/__init__.py#_40-53

(This can wait for another PR though)

We likely do not want to query FAS to run our tests :)

Couple of comments but nothing blocking this from being merged imho, so :thumbsup: for me

Pull-Request has been merged by skrzepto

7 years ago