<?xml version="1.0"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>CKAN: Ticket #2605: Problem with user.get_groups</title>
    <link>http://localhost/ticket/2605</link>
    <description>&lt;p&gt;
From DR at DGU
&lt;/p&gt;
&lt;p&gt;
Basically get_groups() appears to cache its results, but this falls
down when subsequent calls are in a different session. We get this
when saving a dataset. get_groups() first gets called in the
controller when it is trying to work out what permissions the user
has. It later gets called, and by this time c.userobj is detached, so
get_groups() fails.
&lt;/p&gt;
&lt;p&gt;
I'm working around this by refreshing c.userobj (from c.user) before
calling get_groups(), but I wonder if the get_groups caching could
detect the detached session and bypass the cache in this case?
&lt;/p&gt;
&lt;p&gt;
diff --git a/ckan/tests/models/test_user.py b/ckan/tests/models/test_user.py
index a49759a..ffd3449 100644
--- a/ckan/tests/models/test_user.py
+++ b/ckan/tests/models/test_user.py
@@ -52,3 +52,50 @@ class &lt;a class="missing wiki"&gt;TestUser?&lt;/a&gt;:
&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;
out = model.User.get(u'http:/sandra.owndomain.com/')
assert out
assert out.fullname == u'Sandra'
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
+
+def to_names(domain_obj_list):
+    &lt;strong&gt;Takes a list of domain objects and returns a corresponding list
+    of their names.&lt;/strong&gt;
+    objs = []
+    for obj in domain_obj_list:
+        objs.append(obj.name if obj else None)
+    return objs
+
+class &lt;a class="missing wiki"&gt;TestUserGroups?&lt;/a&gt;:
+    @classmethod
+    def setup_class(self):
+        &lt;a class="missing wiki"&gt;CreateTestData?&lt;/a&gt;.create_arbitrary([{'name': 'testpkg'}],
+                                        extra_user_names=['brian', 'sandra'])
+        &lt;a class="missing wiki"&gt;CreateTestData?&lt;/a&gt;.create_groups([
+            {'name': 'grp1',
+             'phone': '1234',
+             }
+            ])
+        model.repo.new_revision()
+        grp1 = model.Group.by_name(u'grp1')
+        brian = model.User.by_name(u'brian')
+        model.Session.add(model.Member(group=grp1,
+                                       table_id=brian.id,
+                                       table_name='user',
+                                       capacity='admin')
+                         )
+        model.repo.commit_and_remove()
+
+    @classmethod
+    def teardown_class(self):
+        model.repo.rebuild_db()
+
+    def test_get_groups(self):
+        brian = model.User.by_name(u'brian')
+        groups = brian.get_groups()
+        assert_equal(to_names(groups), &lt;a class="missing wiki"&gt;grp1?&lt;/a&gt;)
+        assert_equal(groups&lt;a class="missing changeset" title="No default repository defined"&gt;[0]&lt;/a&gt;.extras, {'phone': '1234'})
+
+        # check cache works between sessions
+        model.Session.expunge_all()
+        #don't refresh brian user since this is how c.user works
+        # i.e. don't do this: brian = model.User.by_name(u'brian')
+        groups = brian.get_groups()
+        assert_equal(to_names(groups), &lt;a class="missing wiki"&gt;grp1?&lt;/a&gt;)
+        assert_equal(groups&lt;a class="missing changeset" title="No default repository defined"&gt;[0]&lt;/a&gt;.extras, {'phone': '1234'})
+
&lt;/p&gt;
</description>
    <language>en-us</language>
    <image>
      <title>CKAN</title>
      <url>http://assets.okfn.org/p/ckan/img/ckan_logo_shortname.png</url>
      <link>http://localhost/ticket/2605</link>
    </image>
    <generator>Trac 0.12.3</generator>
    <item>
      
        <dc:creator>ross</dc:creator>

      <pubDate>Wed, 27 Jun 2012 10:30:46 GMT</pubDate>
      <title>status, description changed</title>
      <link>http://localhost/ticket/2605#comment:1</link>
      <guid isPermaLink="false">http://localhost/ticket/2605#comment:1</guid>
      <description>
          &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;status&lt;/strong&gt;
                changed from &lt;em&gt;new&lt;/em&gt; to &lt;em&gt;accepted&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;description&lt;/strong&gt;
              modified (&lt;a href="/ticket/2605?action=diff&amp;amp;version=1"&gt;diff&lt;/a&gt;)
            &lt;/li&gt;
          &lt;/ul&gt;
      </description>
      <category>Ticket</category>
    </item><item>
      
        <dc:creator>ross</dc:creator>

      <pubDate>Thu, 28 Jun 2012 09:01:50 GMT</pubDate>
      <title></title>
      <link>http://localhost/ticket/2605#comment:2</link>
      <guid isPermaLink="false">http://localhost/ticket/2605#comment:2</guid>
      <description>
        &lt;p&gt;
Seeing *very* strange behaviour here with the tests working fine when run individually, but when run as part of the entire suite it is returning an old group (called david) that is NOT in the database (set breakpoint, queried db, not there).
&lt;/p&gt;
&lt;p&gt;
Hoping Mr Raznicks sqlalchemy skills will be able to see what is going on with this.
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item><item>
      
        <dc:creator>ross</dc:creator>

      <pubDate>Tue, 24 Jul 2012 10:46:07 GMT</pubDate>
      <title></title>
      <link>http://localhost/ticket/2605#comment:3</link>
      <guid isPermaLink="false">http://localhost/ticket/2605#comment:3</guid>
      <description>
        &lt;p&gt;
Make a decision on how we can test this cleanly and get it into 1.8
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item><item>
      
        <dc:creator>ross</dc:creator>

      <pubDate>Tue, 24 Jul 2012 10:56:09 GMT</pubDate>
      <title>status changed; resolution set</title>
      <link>http://localhost/ticket/2605#comment:4</link>
      <guid isPermaLink="false">http://localhost/ticket/2605#comment:4</guid>
      <description>
          &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;status&lt;/strong&gt;
                changed from &lt;em&gt;accepted&lt;/em&gt; to &lt;em&gt;closed&lt;/em&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;resolution&lt;/strong&gt;
                set to &lt;em&gt;fixed&lt;/em&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;p&gt;
Code for the CTE fix is in master ready for 1.8 branch.
&lt;/p&gt;
      </description>
      <category>Ticket</category>
    </item>
 </channel>
</rss>