def setUp(self): addon = Addon.objects.get(pk=3615) self.thread = CommunicationThread(addon=addon) user = UserProfile.objects.all()[0] self.token = CommunicationThreadToken(thread=self.thread, user=user) self.token.modified = datetime.now() self.token.use_count = 0
class TestThreadTokenModel(TestCase): fixtures = ['base/addon_3615', 'base/user_999'] def setUp(self): addon = Addon.objects.get(pk=3615) self.thread = CommunicationThread(addon=addon) user = UserProfile.objects.all()[0] self.token = CommunicationThreadToken(thread=self.thread, user=user) self.token.modified = datetime.now() self.token.use_count = 0 def test_live_thread_token_is_valid(self): """ Test `is_valid()` when the token is fresh (not expired). """ assert self.token.is_valid() def test_expired_thread_token_is_valid(self): """ Test `is_valid()` when the token has expired. """ self.token.modified = self.days_ago(const.THREAD_TOKEN_EXPIRY + 1) assert not self.token.is_valid() def test_unused_token_is_valid(self): """ Test `is_valid()` when the token is unused. """ assert self.token.is_valid() def test_max_used_thread_token_is_valid(self): """ Test `is_valid()` when the token has been fully used. """ self.token.use_count = const.MAX_TOKEN_USE_COUNT assert not self.token.is_valid() def test_reset_uuid(self): """ Test `reset_uuid()` generates a differ uuid. """ self.thread.save() self.token.thread = self.thread self.token.save() uuid = self.token.uuid assert uuid self.token.reset_uuid() assert self.token.uuid assert uuid != self.token.uuid
def create_comm_thread(self, **kwargs): """Create a thread/note objects for communication.""" if not waffle.switch_is_active('comm-dashboard'): return action = kwargs['action'] action_note_types = { 'approve': comm.APPROVAL, 'disable': comm.DISABLED, 'escalate': comm.ESCALATION, 'info': comm.MORE_INFO_REQUIRED, 'comment': comm.REVIEWER_COMMENT, 'reject': comm.REJECTION } thread = CommunicationThread.objects.filter(addon=self.addon, version=self.version) perms = {} for key in self.data['action_visibility']: perms['read_permission_%s' % key] = True if thread.exists(): thread = thread[0] else: thread = CommunicationThread(addon=self.addon, version=self.version, **perms) # Set permissions from the form. thread.save() self.comm_note = CommunicationNote.objects.create( note_type=action_note_types[action], body=self.data['comments'], author=self.request.amo_user, thread=thread, **perms) self.comm_thread = thread moz_emails = self.addon.get_mozilla_contacts() # CC mozilla contact. for email in moz_emails: try: moz_contact = UserProfile.objects.get(email=email) except UserProfile.DoesNotExist: pass else: CommunicationThreadCC.objects.create(thread=thread, user=moz_contact)
def create_comm_note(app, version, author, body, note_type=comm.NO_ACTION, perms=None): """ Creates a note on an app version's thread. Creates a thread if a thread doesn't already exist. CC's app's Mozilla contacts to auto-join thread. app -- app object. version -- app version. author -- UserProfile for the note's author. body -- string/text for note comment. note_type -- integer for note_type (mkt constant), defaults to 0/NO_ACTION (e.g. comm.APPROVAL, comm.REJECTION, comm.NO_ACTION). perms -- object of groups to grant permission to, will set flags on Thread. (e.g. {'developer': False, 'staff': True}). """ if not waffle.switch_is_active('comm-dashboard'): return None, None # Dict of {'read_permission_GROUP_TYPE': boolean}. # Perm for dev, reviewer, senior_reviewer, moz_contact, staff all True by # default. perms = perms or {} create_perms = dict(('read_permission_%s' % key, has_perm) for key, has_perm in perms.iteritems()) # Get or create thread w/ custom permissions. thread = None threads = app.threads.filter(version=version) if threads.exists(): thread = threads[0] else: # See if user has perms to create thread for this app. thread = CommunicationThread(addon=app, version=version, **create_perms) if user_has_perm_thread(thread, author): thread.save() else: raise PermissionDenied # Create note. note = thread.notes.create(note_type=note_type, body=body, author=author, **create_perms) post_create_comm_note(note) return thread, note
def create_comm_thread(self, **kwargs): """Create a thread/note objects for communication.""" if not waffle.switch_is_active('comm-dashboard'): return action = kwargs['action'] action_note_types = { 'approve': comm.APPROVAL, 'disable': comm.DISABLED, 'escalate': comm.ESCALATION, 'info': comm.MORE_INFO_REQUIRED, 'comment': comm.REVIEWER_COMMENT, 'reject': comm.REJECTION } thread = CommunicationThread.objects.filter(addon=self.addon, version=self.version) perms = {} for key in self.data['action_visibility']: perms['read_permission_%s' % key] = True if thread.exists(): thread = thread[0] else: thread = CommunicationThread(addon=self.addon, version=self.version, **perms) # Set permissions from the form. thread.save() self.comm_note = CommunicationNote.objects.create( note_type=action_note_types[action], body=self.data['comments'], author=self.request.amo_user, thread=thread, **perms) self.comm_thread = thread moz_emails = self.addon.get_mozilla_contacts() # CC mozilla contact. for email in moz_emails: try: moz_contact = UserProfile.objects.get(email=email) except UserProfile.DoesNotExist: pass else: CommunicationThreadCC.objects.create( thread=thread, user=moz_contact)