summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKelley Spoon <kelley.spoon@linaro.org>2021-07-22 11:46:57 -0500
committerKelley Spoon <kelley.spoon@linaro.org>2021-09-21 07:30:00 -0500
commit2062d058bcde7133fe4bb48b427be98274509daf (patch)
tree9f90c2cb29aecfd86a43161a1faad0daa484b1d8
parenta7a6a3cf40ac9aa598a42368c1b9e8e0027cde9c (diff)
crowd: fully remove crowd from patchwork
Since we don't really need it anymore, let's drop crowd, clean up scripts that were relying on it, and fix our tests so they aren't trying to mock it. Ensure that tests are working correctly with the new Person assumption and that we are detecting Linaro involvement in all situations. Change-Id: I029c482013e9745c591a78f5186c1bf404661428
-rw-r--r--linaro_metrics/backends.py11
-rw-r--r--linaro_metrics/crowd.py154
-rw-r--r--linaro_metrics/crowdwrap.py35
-rw-r--r--linaro_metrics/parsemail.py57
-rw-r--r--linaro_metrics/settings.py5
-rwxr-xr-xlinaro_metrics/sync_github_changes.py16
-rw-r--r--linaro_metrics/team_project_credit.py12
-rw-r--r--linaro_metrics/tests/test_crowd.py76
-rw-r--r--linaro_metrics/tests/test_sync_teams.py147
-rw-r--r--linaro_metrics/tests/test_views.py41
-rw-r--r--setup.cfg6
-rw-r--r--tests/test_import_emails.py116
-rw-r--r--tests/test_update_commited_patches.py38
13 files changed, 248 insertions, 466 deletions
diff --git a/linaro_metrics/backends.py b/linaro_metrics/backends.py
deleted file mode 100644
index 12b168c..0000000
--- a/linaro_metrics/backends.py
+++ /dev/null
@@ -1,11 +0,0 @@
-from crowdrest.backend import CrowdRestBackend
-
-
-class LowerCaseCrowdBackend(CrowdRestBackend):
- def create_or_update_user(self, user_id):
- return super(LowerCaseCrowdBackend, self).create_or_update_user(
- user_id.lower())
-
- def authenticate(self, username=None, password=None):
- return super(LowerCaseCrowdBackend, self).authenticate(
- username.lower(), password)
diff --git a/linaro_metrics/crowd.py b/linaro_metrics/crowd.py
deleted file mode 100644
index 65eec89..0000000
--- a/linaro_metrics/crowd.py
+++ /dev/null
@@ -1,154 +0,0 @@
-# Copyright (C) 2013 Linaro
-#
-# Author: Milo Casagrande <milo.casagrande@linaro.org>
-# This file is part of the Patchmetrics package.
-#
-# Patchmetrics is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# Patchmetrics is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Patchwork; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
-import base64
-import contextlib
-import fcntl
-import httplib
-import json
-import time
-import urllib
-
-
-class CrowdException(Exception):
- """Base class for Crowd exceptions."""
-
-
-class CrowdNotFoundException(CrowdException):
- """An exception for 404 status."""
-
-
-class CrowdForbiddenException(CrowdException):
- """An exception for 403 status."""
-
-
-class CrowdUnauthorizedException(CrowdException):
- """ An exception for 401 status."""
-
-
-class Crowd(object):
- """A Crowd object used to perform query operations."""
-
- def __init__(self, usr, pwd, url):
- self._cache = None
- self._usr = usr
- self._pwd = pwd
- assert url.startswith('https://')
- _, _, self._host, self._uri = url.split('/', 3)
- if ":" in self._host:
- self._host, self._port = self._host.split(':')
- else:
- self._port = 443
-
- self._auth = base64.encodestring(
- '%s:%s' % (self._usr, self._pwd)).strip()
- self._headers = {
- 'Authorization': 'Basic ' + self._auth,
- 'Accept': 'application/json'
- }
-
- @contextlib.contextmanager
- def cached(self, cache_file):
- '''provide a cached version of the api to speed things up'''
- with open(cache_file, 'a+') as f:
- try:
- fcntl.lockf(f, fcntl.LOCK_EX)
- f.seek(0)
- try:
- self._cache = json.load(f)
- except Exception:
- self._cache = {} # in case things are corrupted
-
- yield
- self._cache_clean()
-
- f.truncate(0)
- f.seek(0)
- json.dump(self._cache, f)
- finally:
- fcntl.lockf(f, fcntl.LOCK_UN)
- self._cache = None
-
- def _cached(self, email):
- if self._cache is None:
- return None
- user = self._cache.get(email)
- if user and user['expires'] < time.time():
- return None
- return user
-
- def _cache_user(self, email, valid):
- if self._cache is None:
- return None
- self._cache[email] = {
- 'email': email,
- 'valid': valid,
- 'expires': time.time() + 60 * 60 * 24 * 7 # one week
- }
-
- def _cache_clean(self):
- # remove stale entries so file doesn't grow out of hand
- now = time.time()
- self._cache = {k: v for (k, v) in self._cache.iteritems()
- if v['expires'] > now}
-
- def get_user_no_cache(self, email):
- params = {'username': email.encode('utf-8')}
- resource = '/user?{0}'.format(urllib.urlencode(params))
- try:
- resp = json.loads(self._get_rest_usermanagement(resource))
- except CrowdNotFoundException:
- resp = None
- return resp
-
- def user_valid(self, email):
- user = self._cached(email)
- if user:
- return user['valid']
- valid = self.get_user_no_cache(email) is not None
- self._cache_user(email, valid)
- return valid
-
- def get_group(self, grp):
- resource = '/group/user/nested?' + urllib.urlencode({'groupname': grp})
- users = json.loads(self._get_rest_usermanagement(resource))['users']
- return [x['name'] for x in users]
-
- def _get_rest_usermanagement(self, resource):
- api_url = "/{0}{1}".format(self._uri, resource)
- return self._get(api_url)
-
- def _get(self, api_url):
- connection = httplib.HTTPSConnection(self._host, self._port)
- connection.request("GET", api_url, headers=self._headers)
- resp = connection.getresponse()
-
- if resp.status == 200:
- return resp.read()
- elif resp.status == 404:
- raise CrowdNotFoundException('Resource not found')
- elif resp.status == 401:
- raise CrowdUnauthorizedException(
- 'Authorization not granted to fulfill the request')
- elif resp.status == 403:
- raise CrowdForbiddenException(
- 'Access forbidden to fulfill the request')
- else:
- raise CrowdException(
- 'Unknown Crowd status {0}'.format(resp.status))
diff --git a/linaro_metrics/crowdwrap.py b/linaro_metrics/crowdwrap.py
deleted file mode 100644
index 147d41a..0000000
--- a/linaro_metrics/crowdwrap.py
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/env python
-import os
-import sys
-
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
-sys.path.append("/srv/linaro-git-tools")
-
-from linaro_ldap import get_employees_by_team
-from linaro_metrics.crowd import CrowdNotFoundException
-
-PREFER_LDAP = ['LDCG']
-
-
-class CrowdWrap(object):
-
- def get_group(self, crowd, grp):
- try:
- # in cases where there's actually a posix group
- # with the same name as the teamname, we need
- # to just skip crowd and go to LDAP.
- if grp not in PREFER_LDAP:
- return crowd.get_group(grp)
- else:
- raise CrowdNotFoundException
- except CrowdNotFoundException:
- # not in crowd, let's check LDAP
- ldap_teams = get_employees_by_team()
- for dept in ldap_teams:
- if (grp in ldap_teams[dept]):
- # the function returns all LDAP data, so strip
- # out just the 'mail' attribute for each entry
- return [x["mail"] for x in ldap_teams[dept][grp]]
-
- # if we're still here, the group wasn't found
- raise CrowdNotFoundException('Group "%s" not found' % grp)
diff --git a/linaro_metrics/parsemail.py b/linaro_metrics/parsemail.py
index 5c5cd43..d5817b6 100644
--- a/linaro_metrics/parsemail.py
+++ b/linaro_metrics/parsemail.py
@@ -9,9 +9,6 @@ from django.conf import settings
from patchwork.models import Patch, Person, Project
from patchwork.parser import parse_patch, subject_check
-from linaro_metrics.crowd import Crowd
-from linaro_metrics.sync_teams import get_or_create_person
-
log = logging.getLogger('import_emails')
@@ -97,21 +94,17 @@ def find_author_submitter(mail, comment_lines):
return auth, submitter
-def get_linaro_person(crowd, email):
- # the author is linaro
- if crowd.user_valid(email):
- # we need to make sure the "user" exists so that we can apply team
- # credits in _patch_saved_callback
- return get_or_create_person(crowd, email)
+def get_linaro_person(email):
+ p = Person.objects.filter(email=email)
+ if p.count() > 0:
+ pers = p.first()
+ if pers.user and pers.user.is_active is True:
+ return pers
- # the author has linked a non-linaro email to their User
- p = Person.objects.filter(email__iexact=email)
- if p.count() == 1 and p[0].user:
- if crowd.user_valid(p[0].user.email):
- return p[0]
+ return None
-def linaro_parse_mail(crowd, real_parse_mail, mail):
+def linaro_parse_mail(real_parse_mail, mail):
'''Only track patches authored or submitted by "linaro" people. We
determine this by first finding the author's email. Most of the time
this is the simply the "from" address. Occasionally the "from" winds up
@@ -119,7 +112,7 @@ def linaro_parse_mail(crowd, real_parse_mail, mail):
content. Once we have the author email address we check the following
logic:
- * is this author or submitter a valid crowd email?
+ * is this author or submitter a User in patchwork?
* is there a Person in patchwork by this email address?
- if so, is its User part of Linaro? (ie allow tracking of non-linaro
emails
@@ -136,7 +129,8 @@ def linaro_parse_mail(crowd, real_parse_mail, mail):
comment_lines, patch = find_comment_and_patch(mail)
author, submitter = find_author_submitter(mail, comment_lines)
- person = get_linaro_person(crowd, author)
+
+ person = get_linaro_person(author)
if person:
# we have a linaro authored patch
@@ -148,29 +142,26 @@ def linaro_parse_mail(crowd, real_parse_mail, mail):
else:
# see if its a linaro submitter, we'll add the patch but not give
# out team credits
- submitter = get_linaro_person(crowd, submitter)
- if submitter:
+ sub = get_linaro_person(submitter)
+ if sub:
Patch.linaro_author = person
return real_parse_mail(mail)
-
return 0
@contextlib.contextmanager
def monkey_patcher(parser):
- crwd = Crowd(settings.CROWD_USER, settings.CROWD_PASS, settings.CROWD_URL)
def_project, _ = Project.objects.get_or_create(
linkname=settings.DEFAULT_PROJECT)
- with crwd.cached(settings.CROWD_CACHE):
- orig_find = parser.find_project_by_header
- orig_parse = parser.parse_mail
- try:
- parser.find_project_by_header = functools.partial(
- linaro_find_project, orig_find, def_project)
- parser.parse_mail = functools.partial(
- linaro_parse_mail, crwd, orig_parse)
- yield
- finally:
- parser.parse_mail = orig_parse
- parser.find_project_by_header = orig_find
+ orig_find = parser.find_project_by_header
+ orig_parse = parser.parse_mail
+ try:
+ parser.find_project_by_header = functools.partial(
+ linaro_find_project, orig_find, def_project)
+ parser.parse_mail = functools.partial(
+ linaro_parse_mail, orig_parse)
+ yield
+ finally:
+ parser.parse_mail = orig_parse
+ parser.find_project_by_header = orig_find
diff --git a/linaro_metrics/settings.py b/linaro_metrics/settings.py
index ee70ad5..b0bf450 100644
--- a/linaro_metrics/settings.py
+++ b/linaro_metrics/settings.py
@@ -28,11 +28,6 @@ SECRET_KEY = '00000000000000000000000000000000000000000000000000'
DEBUG = True
TEMPLATE_DEBUG = True
-CROWD_USER = None
-CROWD_PASS = None
-CROWD_URL = None
-CROWD_CACHE = '/tmp/crowd_cache.json'
-
INSTALLED_APPS.append('linaro_metrics') # NOQA F405
DEFAULT_TEAM = 'no-team'
DEFAULT_PROJECT = 'no-project'
diff --git a/linaro_metrics/sync_github_changes.py b/linaro_metrics/sync_github_changes.py
index 90e7b6f..5967f07 100755
--- a/linaro_metrics/sync_github_changes.py
+++ b/linaro_metrics/sync_github_changes.py
@@ -20,7 +20,6 @@ from django.conf import settings
from patchwork.models import Patch, Project, State
-from linaro_metrics.crowd import Crowd
from linaro_metrics.models import TeamCredit
from linaro_metrics.parsemail import get_linaro_person
from linaro_metrics import team_project_credit
@@ -158,12 +157,12 @@ def get_commits(pull_request):
return json.loads(resp.read())
-def get_author(crowd, commits):
+def get_author(commits):
if not len(commits):
# some PR's have no commits: https://github.com/docker/docker/pull/5894
return
email = commits[0]['commit']['author']['email']
- return get_linaro_person(crowd, email)
+ return get_linaro_person(email)
def patchwork_state(github_status):
@@ -231,18 +230,17 @@ def repo_cache():
json.dump(data, f, default=dt_serialize)
-def create_tags(crowd, project, commits):
+def create_tags(project, commits):
for commit in commits:
c = Commit(commit['sha'], commit['commit']['message'],
commit['commit']['author'])
team_project_credit.update_commit_callback(
- crowd, project, None, c, False)
+ project, None, c, False)
def main(args):
- crwd = Crowd(settings.CROWD_USER, settings.CROWD_PASS, settings.CROWD_URL)
- with crwd.cached(settings.CROWD_CACHE), repo_cache() as repos:
+ with repo_cache() as repos:
for owner, repo, proj in GITHUB_REPOS:
repo_path = '%s/%s' % (owner, repo)
log.info('Looking at: %s', repo_path)
@@ -253,12 +251,12 @@ def main(args):
for pr in get_pull_requests(owner, repo, last_update):
x += 1
commits = get_commits(pr)
- auth = get_author(crwd, commits)
+ auth = get_author(commits)
if auth:
log.info('checking change: %d', pr['number'])
create_or_update(proj, owner, repo, auth, pr)
project = Project.objects.get(name=proj)
- create_tags(crwd, project, commits)
+ create_tags(project, commits)
repos[repo_path] = now
finally:
log.info('analayzed %d pull-requests', x)
diff --git a/linaro_metrics/team_project_credit.py b/linaro_metrics/team_project_credit.py
index d1e973e..5c080b8 100644
--- a/linaro_metrics/team_project_credit.py
+++ b/linaro_metrics/team_project_credit.py
@@ -6,7 +6,6 @@ import logging
from django.conf import settings
-from linaro_metrics.crowd import Crowd
from linaro_metrics.parsemail import get_linaro_person
from linaro_metrics.models import (
CommitTagCredit,
@@ -22,10 +21,10 @@ response_re = \
r' .*<(.*@.*)>$', re.M | re.I)
-def update_commit_callback(crowd, project, repo, commit, dryrun):
+def update_commit_callback(project, repo, commit, dryrun):
for match in response_re.finditer(commit.message):
tag, email = match.groups()
- p = get_linaro_person(crowd, email)
+ p = get_linaro_person(email)
if p:
log.debug('User %s found with tag %s', p, tag)
if not dryrun:
@@ -47,8 +46,5 @@ def update_commit_callback(crowd, project, repo, commit, dryrun):
@contextlib.contextmanager
def update_commit_callback_constructor():
- crwd = Crowd(settings.CROWD_USER, settings.CROWD_PASS, settings.CROWD_URL)
-
- with crwd.cached(settings.CROWD_CACHE):
- cb = functools.partial(update_commit_callback, crwd)
- yield cb
+ cb = functools.partial(update_commit_callback)
+ yield cb
diff --git a/linaro_metrics/tests/test_crowd.py b/linaro_metrics/tests/test_crowd.py
deleted file mode 100644
index 9e5cc34..0000000
--- a/linaro_metrics/tests/test_crowd.py
+++ /dev/null
@@ -1,76 +0,0 @@
-import json
-import os
-import tempfile
-import time
-import unittest
-
-from linaro_metrics.crowd import Crowd
-
-
-class TestCrowdCache(unittest.TestCase):
- def setUp(self):
- _, self.tmpfile = tempfile.mkstemp(prefix='crowdtest')
- self.addCleanup(os.unlink, self.tmpfile)
- self.crowd = Crowd('user', 'pass', 'https://foo/bar/server')
-
- def fake_get(api_url):
- return '''{"email": "email"}'''
- self.crowd._get = fake_get
-
- def test_unicode(self):
- '''Ensure we can handle unicode characters in an email address'''
- # a real commit in linux.git where the author has a unicode character:
- # https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
- # commit/?id=e82661e23c60fc41424ca138820d729d8e4a2226
- self.crowd.get_user_no_cache(u'samuel.pitoiset\u0153gmail.com')
-
- def test_corrupted_cache(self):
- with open(self.tmpfile, 'w') as f:
- f.write('invalid json')
- with self.crowd.cached(self.tmpfile):
- self.assertTrue(self.crowd.user_valid('foo@bar.com'))
- with open(self.tmpfile) as f:
- self.assertIn('foo@bar.com', json.load(f))
-
- def test_cache_hit(self):
- data = {
- 'foo@bar.com': {
- 'email': 'foo',
- 'valid': False,
- 'expires': time.time() + 100,
- }
- }
- with open(self.tmpfile, 'w') as f:
- json.dump(data, f)
- with self.crowd.cached(self.tmpfile):
- self.assertFalse(self.crowd.user_valid('foo@bar.com'))
-
- def test_cache_miss(self):
- data = {
- 'foo@bar.com': {
- 'email': 'foo',
- 'expires': time.time() - 1,
- }
- }
- with open(self.tmpfile, 'w') as f:
- json.dump(data, f)
- with self.crowd.cached(self.tmpfile):
- self.assertTrue(self.crowd.user_valid('foo@bar.com'))
-
- def test_cache_clean(self):
- data = {
- 'foo@bar.com': {
- 'email': 'email',
- 'expires': time.time() - 1,
- },
- 'FOO@bar.com': {
- 'email': 'email',
- 'expires': time.time() + 100,
- }
- }
- with open(self.tmpfile, 'w') as f:
- json.dump(data, f)
- with self.crowd.cached(self.tmpfile):
- pass
- with open(self.tmpfile) as f:
- self.assertEqual(1, len(json.load(f).keys()))
diff --git a/linaro_metrics/tests/test_sync_teams.py b/linaro_metrics/tests/test_sync_teams.py
index f5778d0..7a0c6e5 100644
--- a/linaro_metrics/tests/test_sync_teams.py
+++ b/linaro_metrics/tests/test_sync_teams.py
@@ -1,74 +1,73 @@
-from django.contrib.auth.models import User
-from django.test import TestCase
-from patchwork.models import Person
-
-from linaro_metrics.models import Team, TeamMembership
-from linaro_metrics.sync_teams import get_or_create_person, sync_crowd
-
-
-class FakeCrowd(object):
- def __init__(self):
- self.groups = {}
-
- def add_group(self, name, users):
- self.groups[name] = users
-
- def get_group(self, name):
- return [x[0] for x in self.groups[name]]
-
- def get_user_no_cache(self, user_email):
- for users in self.groups.values():
- for email, display_name in users:
- if user_email == email:
- return {'display-name': display_name}
-
-
-class TestSyncTeams(TestCase):
- def test_user_created(self):
- t = Team.objects.create(name='foo')
- crowd = FakeCrowd()
- crowd.add_group(t.name, [('user@foo.com', 'user name')])
- sync_crowd(crowd, [t])
-
- p = Person.objects.get(email='user@foo.com')
- self.assertEquals('user name', p.name)
- self.assertEquals(p.email, p.user.email)
- tm = TeamMembership.objects.all()[0]
- self.assertEquals('foo', tm.team.name)
- self.assertEquals(p.user, tm.user)
-
- def test_memberships_change(self):
- crowd = FakeCrowd()
- crowd.add_group('foo', [('user@foo.com', 'user name')])
- crowd.add_group('bam', [('user@foo.com', 'user name')])
- u = get_or_create_person(crowd, 'user@foo.com').user
-
- foo = Team.objects.create(name='foo')
- bar = Team.objects.create(name='bar')
- bam = Team.objects.create(name='bam')
-
- # put user in foo and bar
- TeamMembership.objects.create(team=foo, user=u)
- TeamMembership.objects.create(team=bar, user=u)
-
- # sync - and we should only be in foo and bam
- sync_crowd(crowd, [foo, bam])
- teams = [x.team.name for x in TeamMembership.objects.all()]
- self.assertEqual(['foo', 'bam'], teams)
-
- def test_person_no_user(self):
- crowd = FakeCrowd()
- crowd.add_group('foo', [('user@foo.com', 'user name')])
- Person.objects.create(name='user name', email='user@foo.com')
- u = get_or_create_person(crowd, 'user@foo.com').user
- p = Person.objects.get(email='user@foo.com')
- self.assertEqual(u, p.user)
-
- def test_user_no_person(self):
- crowd = FakeCrowd()
- crowd.add_group('foo', [('user@foo.com', 'user name')])
-
- orig = User.objects.create_user(
- 'user name', 'user@foo.com', password=None)
- self.assertEqual(
- orig, get_or_create_person(crowd, 'user@foo.com').user)
+# from django.contrib.auth.models import User
+# from django.test import TestCase
+# from patchwork.models import Person
+
+# from linaro_metrics.models import Team, TeamMembership
+
+
+# class FakeCrowd(object):
+# def __init__(self):
+# self.groups = {}
+#
+# def add_group(self, name, users):
+# self.groups[name] = users
+#
+# def get_group(self, name):
+# return [x[0] for x in self.groups[name]]
+#
+# def get_user_no_cache(self, user_email):
+# for users in self.groups.values():
+# for email, display_name in users:
+# if user_email == email:
+# return {'display-name': display_name}
+
+
+# class TestSyncTeams(TestCase):
+# def test_user_created(self):
+# t = Team.objects.create(name='foo')
+# crowd = FakeCrowd()
+# crowd.add_group(t.name, [('user@foo.com', 'user name')])
+# sync_crowd(crowd, [t])
+#
+# p = Person.objects.get(email='user@foo.com')
+# self.assertEquals('user name', p.name)
+# self.assertEquals(p.email, p.user.email)
+# tm = TeamMembership.objects.all()[0]
+# self.assertEquals('foo', tm.team.name)
+# self.assertEquals(p.user, tm.user)
+#
+# def test_memberships_change(self):
+# crowd = FakeCrowd()
+# crowd.add_group('foo', [('user@foo.com', 'user name')])
+# crowd.add_group('bam', [('user@foo.com', 'user name')])
+# u = get_or_create_person(crowd, 'user@foo.com').user
+#
+# foo = Team.objects.create(name='foo')
+# bar = Team.objects.create(name='bar')
+# bam = Team.objects.create(name='bam')
+#
+# # put user in foo and bar
+# TeamMembership.objects.create(team=foo, user=u)
+# TeamMembership.objects.create(team=bar, user=u)
+#
+# # sync - and we should only be in foo and bam
+# sync_crowd(crowd, [foo, bam])
+# teams = [x.team.name for x in TeamMembership.objects.all()]
+# self.assertEqual(['foo', 'bam'], teams)
+#
+# def test_person_no_user(self):
+# crowd = FakeCrowd()
+# crowd.add_group('foo', [('user@foo.com', 'user name')])
+# Person.objects.create(name='user name', email='user@foo.com')
+# u = get_or_create_person(crowd, 'user@foo.com').user
+# p = Person.objects.get(email='user@foo.com')
+# self.assertEqual(u, p.user)
+#
+# def test_user_no_person(self):
+# crowd = FakeCrowd()
+# crowd.add_group('foo', [('user@foo.com', 'user name')])
+#
+# orig = User.objects.create_user(
+# 'user name', 'user@foo.com', password=None)
+# self.assertEqual(
+# orig, get_or_create_person(crowd, 'user@foo.com').user)
diff --git a/linaro_metrics/tests/test_views.py b/linaro_metrics/tests/test_views.py
index 9ec0bf0..6fdfb8b 100644
--- a/linaro_metrics/tests/test_views.py
+++ b/linaro_metrics/tests/test_views.py
@@ -1,25 +1,42 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase, skipUnlessDBFeature
-from linaro_metrics.models import Team
-from linaro_metrics.sync_teams import sync_crowd
-from linaro_metrics.tests.test_sync_teams import FakeCrowd
+from linaro_metrics.models import Team, TeamMembership
class TestTeamsView(TestCase):
fixtures = ['default_states']
def setUp(self):
- # Create team memberships and project for a patch we'll import.
+ user1 = User.objects.create(
+ email='user@foo.com',
+ username='user',
+ first_name='user',
+ last_name='name',
+ is_active=True
+ )
+
+ user2 = User.objects.create(
+ email='user2@foo.com',
+ username='user2',
+ first_name='user2',
+ last_name='name',
+ is_active=True
+ )
+
+ user3 = User.objects.create(
+ email='zoltan.kiss@linaro.org',
+ username='zoltan.kiss',
+ first_name='Zoltan',
+ last_name='Kiss',
+ is_active=True
+ )
+
t = Team.objects.create(name='foo')
- crowd = FakeCrowd()
- foo_group = [
- ('user@foo.com', 'user name'),
- ('user2@foo.com', 'user2 name'),
- ('zoltan.kiss@linaro.org', 'Zoltan Kiss'),
- ]
- crowd.add_group('foo', foo_group)
- sync_crowd(crowd, [t])
+
+ TeamMembership.objects.create(team=t, user=user1)
+ TeamMembership.objects.create(team=t, user=user2)
+ TeamMembership.objects.create(team=t, user=user3)
# This can't be run against sqlite3, so it won't get hit by unit-test.sh.
@skipUnlessDBFeature('supports_mixed_date_datetime_comparisons')
diff --git a/setup.cfg b/setup.cfg
index 65dae32..a935487 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,3 +1,8 @@
+[tool:pytest]
+log_cli=True
+log_level=INFO
+addopts=-x -v
+
[flake8]
ignore = E402,W503
exclude =
@@ -6,4 +11,5 @@ exclude =
.venv
per-file-ignores =
linaro_metrics/cli.py: F401,
+ linaro_metrics/team_project_credit.py: F401
linaro_metrics/sync_users.py: F401
diff --git a/tests/test_import_emails.py b/tests/test_import_emails.py
index f576818..d1f57c2 100644
--- a/tests/test_import_emails.py
+++ b/tests/test_import_emails.py
@@ -6,9 +6,9 @@ import import_emails
from django.conf import settings
from django.test import TestCase
+from django.contrib.auth.models import User
from patchwork.models import Comment, Patch, Person, Project, State
from linaro_metrics.models import Team, TeamCredit, TeamMembership
-from linaro_metrics.sync_teams import get_or_create_person
import mock
@@ -42,12 +42,69 @@ class ImapFake(object):
class TestImportEmail(TestCase):
fixtures = ['default_states']
+ def tearDown(self):
+ super(TestImportEmail, self).tearDown()
+ Person.objects.all().delete()
+ User.objects.all().delete()
+
def setUp(self):
super(TestImportEmail, self).setUp()
self.tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tmpdir)
+ self.user = User.objects.create(
+ email='user.name@linaro.org',
+ username='user.name',
+ first_name='User',
+ last_name='Name',
+ is_active=True
+ )
+
+ self.user2 = User.objects.create(
+ email='peter.maydell@linaro.org',
+ username='peter.maydell',
+ first_name='Peter',
+ last_name='Maydell',
+ is_active=True
+ )
+
+ self.person = Person.objects.create(
+ email='user.name@linaro.org',
+ name='User Name',
+ user=self.user
+ )
+
+ self.person2 = Person.objects.create(
+ email='user.name@kernel.org',
+ name='User Name',
+ user=self.user
+ )
+
+ self.person3 = Person.objects.create(
+ email='peter.maydell@linaro.org',
+ name='Peter Maydell',
+ user=self.user2
+ )
+
+ self.person_other = Person.objects.create(
+ email='robh@kernel.org',
+ name='Rob Herring',
+ user=self.user
+ )
+
+ self.person_olof = Person.objects.create(
+ email='olof@lixom.net',
+ name='Olof Johansson',
+ user=self.user2
+ )
+
+ self.notlinaro_person = Person.objects.create(
+ email='not@notlinaro.org',
+ name='Not Linaro',
+ user=None
+ )
+
p = Project.objects.create(listid='lng-odp.lists.linaro.org')
self.addCleanup(p.delete)
@@ -89,21 +146,25 @@ class TestImportEmail(TestCase):
settings.PARSEMAIL_MONKEY_PATCHER = 'tests.test_import_emails.ImapFake'
self.assertEqual(ImapFake, import_emails.get_monkey_patcher())
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def test_monkey_patch_linaro_only(self, crowd):
+ def test_monkey_patch_linaro_only(self):
'''Test that monkey patching rejects non-linaro patches'''
- crowd().user_valid.return_value = False
Project.objects.all().delete()
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch('non_linaro.mbox')
self.assertEqual(0, Patch.objects.all().count())
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def test_monkey_patch_author_check_user_created(self, crowd):
+ def test_monkey_patch_author_check_user_created(self):
'''Test that we can find the author from the patch comment. The
author is linaro but doesn't exist locally'''
Project.objects.all().delete()
- crowd().get_user_no_cache.return_value = {'display-name': 'User Name'}
+ # junk
+ self.assertEqual(1, User.objects.filter(username='user.name').count())
+ self.assertEqual(
+ Person.objects.filter(email='user.name@linaro.org').first().user,
+ User.objects.filter(username='user.name').first()
+ )
+ # junk
+
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch('author_submitter_differ.mbox')
self.assertEqual(1, Patch.objects.all().count())
@@ -116,26 +177,22 @@ class TestImportEmail(TestCase):
p = Project.objects.get(linkname=settings.DEFAULT_PROJECT)
self.assertEqual(p, patches[0].project)
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def test_monkey_patch_submitter_is_linaro(self, crowd):
+ def test_monkey_patch_submitter_is_linaro(self):
'''A valid Linaro User may submit patches on behalf of a user. We have
users that want to track these patches in our instance, but we
SHOULD NOT contribute these to "team credits"'''
Project.objects.all().delete()
- crowd().get_user_no_cache.return_value = {'display-name': 'User Name'}
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch('applied-patch.mbox')
self.assertEqual(0, Patch.objects.all().count())
tcs = TeamCredit.objects.all()
self.assertEqual(0, tcs.count())
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def test_monkey_patch_maintainer_applied(self, crowd):
+ def test_monkey_patch_maintainer_applied(self):
'''Don't give a patch credit to a maintainer applying a patch to a
tree'''
Project.objects.all().delete()
- crowd().get_user_no_cache.return_value = {'display-name': 'User Name'}
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch('author_not_linaro.mbox')
self.assertEqual(1, Patch.objects.all().count())
@@ -147,17 +204,14 @@ class TestImportEmail(TestCase):
p = Project.objects.get(linkname=settings.DEFAULT_PROJECT)
self.assertEqual(p, patches[0].project)
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def _test_patch_auth(self, patch, author, crowd):
+ def _test_patch_auth(self, patch, author):
Project.objects.all().delete()
- Person.objects.all().delete()
self.auth_found = None
- def get_user_no_cache(email):
- self.auth_found = email
- return {'display-name': 'User Name'}
+ person = Person.objects.filter(email=author)
+ if person.count() > 0:
+ self.auth_found = person[0].email
- crowd().get_user_no_cache = get_user_no_cache
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch(patch)
self.assertEqual(1, Patch.objects.all().count())
@@ -180,31 +234,24 @@ class TestImportEmail(TestCase):
self._test_patch_auth(
'cp8859.mbox', 'user.name@linaro.org')
- with mock.patch('linaro_metrics.parsemail.Crowd') as crowd:
- crowd().user_valid.return_value = False
- with import_emails.get_monkey_patcher()(import_emails.parser):
- self._import_patch('cp8859_comment.mbox')
- self.assertEqual(1, Comment.objects.all().count())
+ with import_emails.get_monkey_patcher()(import_emails.parser):
+ self._import_patch('cp8859_comment.mbox')
+ self.assertEqual(1, Comment.objects.all().count())
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def test_monkey_patch_user_is_linaro(self, crowd):
+ def test_monkey_patch_user_is_linaro(self):
'''A valid Linaro User may submit patches from a non-linaro looking
Person'''
Project.objects.all().delete()
def user_valid(email):
return email.endswith('@linaro.org')
- crowd().user_valid = user_valid
- crowd().get_user_no_cache.return_value = {'display-name': 'User Name'}
- person = get_or_create_person(crowd(), 'user.name@linaro.org')
teams = [
Team.objects.create(name='foo'),
Team.objects.create(name='bar'),
]
for t in teams:
- TeamMembership.objects.create(team=t, user=person.user)
- Person.objects.create(email='robh@kernel.org', user=person.user)
+ TeamMembership.objects.create(team=t, user=self.user)
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch('user_linaro_not_person.mbox')
@@ -212,15 +259,12 @@ class TestImportEmail(TestCase):
tcs = [x.team for x in TeamCredit.objects.all()]
self.assertEqual(teams, tcs)
- @mock.patch('linaro_metrics.parsemail.Crowd')
- def test_monkey_patch_no_listid(self, crowd):
+ def test_monkey_patch_no_listid(self):
Project.objects.all().delete()
qemu = Project.objects.create(
name='qemu-devel', linkname='qemu-devel',
listemail='qemu-devel@nongnu.org', listid='qemu-devel.nongnu.org')
- crowd().get_user_no_cache.return_value = {'display-name': 'User Name'}
-
with import_emails.get_monkey_patcher()(import_emails.parser):
self._import_patch('no-list-id.mbox')
self.assertEqual(1, Patch.objects.all().count())
diff --git a/tests/test_update_commited_patches.py b/tests/test_update_commited_patches.py
index 13f294c..15c97be 100644
--- a/tests/test_update_commited_patches.py
+++ b/tests/test_update_commited_patches.py
@@ -12,8 +12,9 @@ from linaro_metrics.models import (
CommitTagCredit,
TeamMembership,
Team,
+ User,
+ Person
)
-from linaro_metrics.parsemail import get_linaro_person
from tests import TestRepo
import mock
@@ -42,6 +43,21 @@ class TestUpdateCommitedPatches(TestCase):
linkname='patchwork_copy')
self.addCleanup(self.project.delete)
+ self.linaro_user = User.objects.create(
+ email='legit.user@linaro.org',
+ username='legit.user',
+ first_name='Legit',
+ last_name='User',
+ is_active=True)
+ self.addCleanup(self.linaro_user.delete)
+
+ self.linaro_person = Person.objects.create(
+ email='legit.user@linaro.org',
+ name='Legit User',
+ user=self.linaro_user
+ )
+ self.addCleanup(self.linaro_person.delete)
+
commit = self.upstream_repo.add_commit(
'file.txt', 'line1\nline2', 'testing update_commited_patches')
self.patchmail = self.upstream_repo.create_patch(commit)
@@ -110,24 +126,20 @@ class TestUpdateCommitedPatches(TestCase):
commit = mock.Mock()
commit.message = 'Commit message\n' \
- 'Signed-off-by: Foo Bar <foo.bar@linaro.org>'
+ 'Signed-off-by: %s <%s>' % (
+ self.linaro_person.name,
+ self.linaro_person.email)
commit.id = '1234'
- commit.author = 'Foo Bar <foo.bar@linaro.org>'
+ commit.author = '%s <%s>' % (
+ self.linaro_person.name,
+ self.linaro_person.email)
commit.commit_time = time.time()
commit.commit_timezone = -30
- crowd = mock.Mock()
- crowd.user_valid.return_value = True
-
- crowd.get_user_no_cache.return_value = {
- 'display-name': 'foo.bar@linaro.org',
- 'email': 'foo.bar@linaro.org',
- }
- p = get_linaro_person(crowd, 'foo.bar@linaro.org').user
t = Team.objects.create(name='TeamName')
- TeamMembership.objects.create(team=t, user=p)
+ TeamMembership.objects.create(team=t, user=self.linaro_user)
- update_commit_callback(crowd, self.project, None, commit, False)
+ update_commit_callback(self.project, None, commit, False)
credits = CommitTagCredit.objects.all()
self.assertEqual(1, credits.count())