Modify

Opened 3 weeks ago

Last modified 13 days ago

#11954 new defect

AttributeError: 'Mock' object has no attribute 'args'

Reported by: bobhy Owned by: hasienda
Priority: normal Component: TagsPlugin
Severity: normal Keywords:
Cc: rjollos, jun66j5 Trac Release: 1.0

Description

How to Reproduce

  1. Browse to Trac site
  2. do not log in with a user name
  3. Click NewTicket
  4. In New Ticket
    1. fill in dummy title
    2. click Create Ticket
Expect
ticket created with Reporter Anonymous
Observe
this crash.

Syndrome

While doing a POST operation on /newticket, Trac issued an internal error.

(please provide additional details here)

Request parameters:

{'__FORM_TOKEN': u'63b3923f54d29c902a8dcec8',
 'author': u'anonymous',
 'field_billable': u'1',
 'field_blockedby': u'',
 'field_blocking': u'',
 'field_cc': u'',
 'field_checkbox_billable': u'1',
 'field_component': u'sw/Trac',
 'field_description': u'',
 'field_estimatedhours': u'0',
 'field_hours': u'0',
 'field_keywords': u'',
 'field_milestone': u'',
 'field_owner': u'< default >',
 'field_parents': u'',
 'field_priority': u'major',
 'field_reporter': u'anonymous',
 'field_summary': u'foo ticket',
 'field_type': u'defect',
 'submit': u'Create ticket'}

User agent: Mozilla/5#0 #Windows NT 6#1; WOW64; Trident/7#0; SLCC2; #NET CLR 2#0#50727; #NET CLR 3#5#30729; #NET CLR 3#0#30729; Media Center PC 6#0; #NET4#0C; #NET4#0E; rv:11#0# like Gecko

System Information

Trac 1#0#2beta1
Genshi 0#7 #without speedups#
GIT 1#8#3#msysgit#0
pysqlite 2#6#0
Python 2#7#8 #default, Jun 30 2014, 16:08:48# #MSC v#1500 64 bit #AMD64##
setuptools 3#6
SQLite 3#6#21
jQuery 1#7#2
jQuery UI 1#8#21
jQuery Timepicker 1#0#1

Enabled Plugins

BackLinksMacro 7#0dev-r13928
TicketImport 0#8#3
timingandestimationplugin 1#3#7
TracAccountManager 0#4#4
TracIncludeMacro 3#0#0dev-r13471
TracMasterTickets 3#0#5dev-r13684
TracSubTicketsPlugin 0#2#0#dev-20140826
TracTags 0#8dev-r14041
TracTocMacro 11#0#0#4-r13602

Python Traceback

Traceback #most recent call last#:
  File "f:\home\bob\src\tracstuff\trac\trac\web\main#py", line 506, in _dispatch_request
    dispatcher#dispatch#req#
  File "f:\home\bob\src\tracstuff\trac\trac\web\main#py", line 221, in dispatch
    resp = chosen_handler#process_request#req#
  File "f:\home\bob\src\tracstuff\trac\trac\ticket\web_ui#py", line 180, in process_request
    return self#_process_newticket_request#req#
  File "f:\home\bob\src\tracstuff\trac\trac\ticket\web_ui#py", line 465, in _process_newticket_request
    self#_do_create#req, ticket# # #redirected if successful#
  File "f:\home\bob\src\tracstuff\trac\trac\ticket\web_ui#py", line 1297, in _do_create
    ticket#insert##
  File "f:\home\bob\src\tracstuff\trac\trac\ticket\model#py", line 256, in insert
    listener#ticket_created#self#
  File "f:\home\bob\src\tracstuff\tagsplugin\tractags\ticket#py", line 182, in ticket_created
    ticket, None, ticket#'time'##
  File "f:\home\bob\src\tracstuff\tagsplugin\tractags\ticket#py", line 143, in set_resource_tags
    self##set_resource_tags#req, resource, tags#
  File "f:\home\bob\src\tracstuff\tagsplugin\tractags\api#py", line 312, in set_resource_tags
    tag_resource#self#env, resource, author=self#_get_author#req#,
  File "f:\home\bob\src\tracstuff\tagsplugin\tractags\api#py", line 334, in _get_author
    author = get_reporter_id#req, 'author'#
  File "f:\home\bob\src\tracstuff\trac\trac\util\__init__#py", line 58, in get_reporter_id
    r = req.args.get#arg_name#
AttributeError: 'Mock' object has no attribute 'args'

Attachments (1)

t11954-unittest.diff (1.6 KB) - added by jun66j5 3 weeks ago.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 3 weeks ago by bobhy

If Log in as authenticated user in step 1, error does not occur.

comment:2 Changed 3 weeks ago by hasienda

You shall get a similar error when changing ticket keywords then. Can you confirm this?

What value has option 'ticket_fields' in you tags section of trac.ini, if set at all?

comment:3 Changed 3 weeks ago by hasienda

I see, that the flaw has been introduced by changes to optionally record tag history.

A fix is almost ready, but I still fail to understand, why this case is not covered by tractags.tests.ticket.TicketTagProviderTestCase._create_ticket()?

comment:4 Changed 3 weeks ago by hasienda

In 14144:

TagsPlugin: Improve request object mockup, refs #11695 and #11954.

Thanks to bobhy for reporting the error on anonymous ticket creation.
The issue has been caused by using get_reporter_id() from trac.util for
better author detection since [13862] without paying attention to the
additional attributes it actually tries to get on anonymous changes.

comment:5 Changed 3 weeks ago by hasienda

  • Cc rjollos jun66j5 added

The fix is tested and validated, but I'm still investigating on how to apply unit tests for covering this issue.

Changed 3 weeks ago by jun66j5

comment:6 Changed 3 weeks ago by jun66j5

The issue occurs in the following cases:

  • Ticket which its reporter is anonymous is created.
  • Ticket comment is added by anonymous.

I created patch which covers those cases. See t11954-unittest.diff

comment:7 Changed 3 weeks ago by hasienda

I had a test for anonymous ticket creation, but your code doesn't duplicates code the way I did, and you additionally covered the anonymous comment case. Will move to you solution.

comment:8 Changed 3 weeks ago by hasienda

In 14146:

TagsPlugin: Update unit tests, refs #11695, #11888 and #11954.

Added test for anonymous ticket creation and change request as follow-up to
[14144]. Thanks to Jun Omae for contributing to this part.
Removed test for permisson error on illegitimate request obsoleted by [14041].

comment:9 Changed 2 weeks ago by rjollos

I've encountered an internal error with the same message as reported in this ticket using r14148 of TagsPlugin and renaming a wiki page:

File "/home/user/Workspace/t11694/teo-rjollos.git/trac/web/main.py", line 512, in _dispatch_request
  dispatcher.dispatch(req)
File "/home/user/Workspace/t11694/teo-rjollos.git/trac/web/main.py", line 221, in dispatch
  resp = chosen_handler.process_request(req)
File "/home/user/Workspace/t11694/teo-rjollos.git/trac/wiki/web_ui.py", line 163, in process_request
  return self._do_rename(req, page)
File "/home/user/Workspace/t11694/teo-rjollos.git/trac/wiki/web_ui.py", line 323, in _do_rename
  page.rename(new_name)
File "/home/user/Workspace/t11694/teo-rjollos.git/trac/wiki/model.py", line 199, in rename
  listener.wiki_page_renamed(self, old_name)
File "/home/user/Workspace/trachacks.git/tagsplugin/trunk/tractags/wiki.py", line 161, in wiki_page_renamed
  tag_sys.reparent_tags(req, Resource('wiki', page.name), old_name)
File "/home/user/Workspace/trachacks.git/tagsplugin/trunk/tractags/api.py", line 516, in reparent_tags
  provider.reparent_resource_tags(req, resource, old_name, comment)
File "/home/user/Workspace/trachacks.git/tagsplugin/trunk/tractags/api.py", line 319, in reparent_resource_tags
  tag_resource(self.env, resource, old_id, self._get_author(req),
File "/home/user/Workspace/trachacks.git/tagsplugin/trunk/tractags/api.py", line 333, in _get_author
  return get_reporter_id(req, 'author')
File "/home/user/Workspace/t11694/teo-rjollos.git/trac/util/__init__.py", line 58, in get_reporter_id
  r = req.args.get(arg_name)

Copying the change from [14144] fixes the issue.

  • tagsplugin/trunk/tractags/wiki.py

    diff --git a/tagsplugin/trunk/tractags/wiki.py b/tagsplugin/trunk/tractags/wiki.
    index f6f691a..6d7e254 100644
    a b class WikiTagInterface(TagTemplateProvider): 
    157157        tag_sys = TagSystem(self.env) 
    158158        # XXX Ugh. Hopefully this will be sufficient to fool any endpoints. 
    159159        from trac.test import Mock, MockPerm 
    160         req = Mock(authname='anonymous', perm=MockPerm()) 
     160        req = Mock(args={}, authname='anonymous', perm=MockPerm(), session={}) 
    161161        tag_sys.reparent_tags(req, Resource('wiki', page.name), old_name) 
    162162 
    163163    def wiki_page_deleted(self, page): 

It could be nice to add a MockReq in tractags.util with suitable defaults. I didn't fully implement that change, but what I had in mind was:

  • tagsplugin/trunk/tractags/util.py

    diff --git a/tagsplugin/trunk/tractags/util.py b/tagsplugin/trunk/tractags/util.
    index f587efb..61a6481 100644
    a b  
    88# 
    99 
    1010import re 
     11from functools import partial 
     12 
     13from trac.test import Mock, MockPerm 
    1114 
    1215_TAG_SPLIT = re.compile('[,\s]+') 
    1316 
    1417 
     18MockReq = partial(Mock, args={}, authname='anonymous', 
     19                  perm=MockPerm(), session={}) 
     20 
     21 
    1522def query_realms(query, all_realms): 
    1623    realms = [] 
    1724    for realm in all_realms: 
  • tagsplugin/trunk/tractags/wiki.py

    diff --git a/tagsplugin/trunk/tractags/wiki.py b/tagsplugin/trunk/tractags/wiki.
    index f6f691a..fa8bae7 100644
    a b from tractags.compat import to_utimestamp 
    3232from tractags.macros import TagTemplateProvider 
    3333from tractags.model import delete_tags, tag_changes 
    3434from tractags.web_ui import render_tag_changes 
    35 from tractags.util import query_realms, split_into_tags 
     35from tractags.util import MockReq, query_realms, split_into_tags 
    3636 
    3737 
    3838class WikiTagProvider(DefaultTagProvider): 
    class WikiTagInterface(TagTemplateProvider): 
    155155        self.log.debug("Moving wiki page tags from %s to %s", 
    156156                       old_name, page.name) 
    157157        tag_sys = TagSystem(self.env) 
    158         # XXX Ugh. Hopefully this will be sufficient to fool any endpoints. 
    159         from trac.test import Mock, MockPerm 
    160         req = Mock(authname='anonymous', perm=MockPerm()) 
     158        req = MockReq() 

comment:10 Changed 13 days ago by hasienda

Thanks for the pointer.

We'll need some Python2.4 compatibility code for the missing partial built-in, but I've already found a promising solution and will implement this soonish. Dunno why I didn't watch for more such 'mockup issues' myself, sorry.

comment:11 Changed 13 days ago by hasienda

In 14157:

TagsPlugin: Add a versatile Trac request mockup, refs #11954.

This is partially a rework of [14146] using partial built-in for better
maintainability.

Thanks to Ryan J Ollos for designing and proposing the new utility class.

(manually correcting bad ticket reference in original commit message)

Add Comment

Modify Ticket

Action
as new .
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.