# -*- coding: utf-8 -*-
from cubicweb.devtools.testlib import CubicWebTC
from cubicweb.web import uicfg
from cubicweb.web.views import actions, embedding
from cubes.link import views

asf = uicfg.autoform_section

class LinkTC(CubicWebTC):

    def setup_database(self):
        self.request().create_entity('Link', title=u"vous ĂȘtes perdu ?", url=u"http://www.perdu.com")
        
    def test_possible_actions(self):
        req = self.request()
        rset = self.execute('Any X WHERE X is Link')
        self.assertEquals(self.pactionsdict(req, rset),
                             {'mainactions': [actions.ModifyAction, views.LinkFollowAction],
                              'moreactions': [actions.ManagePermissionsAction,actions.DeleteAction,
                                              actions.CopyAction,
                                              embedding.EmbedAction],
                              })

    def test_relations_by_category(self):
        def rbc(iterable):
            return [(rschema.type, x) for rschema, tschemas, x in iterable]
        e = self.vreg["etypes"].etype_class('Link')(self.request())
        # we should only see embed when we are in the managers group
        self.assertEquals(rbc(asf.relations_by_section(e, 'secondary', 'add')),
                          [('embed', 'subject'),
                           ('description', 'subject')])
        self.assertEquals(rbc(asf.relations_by_section(e,'secondary', 'delete')),
                          [('embed', 'subject'),
                           ('description', 'subject')])
        self.create_user('toto')
        self.login('toto')
        e = self.etype_instance('Link') # create a new instance with the new connection
        self.assertEquals(rbc(asf.relations_by_section(e, 'secondary', 'add')),
                          [('description', 'subject')])
        self.assertEquals(rbc(asf.relations_by_section(e, 'secondary', 'delete')),
                          [('description', 'subject')])
        
if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()