1 | """OpenID Provider AX Interface unit tests |
---|
2 | |
---|
3 | NERC DataGrid Project |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "11/11/09" |
---|
7 | __copyright__ = "(C) 2009 Science and Technology Facilities Council" |
---|
8 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
9 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
10 | __revision__ = '$Id$' |
---|
11 | from os import path |
---|
12 | |
---|
13 | from openid.extensions.ax import FetchRequest, FetchResponse, AttrInfo |
---|
14 | |
---|
15 | from ndg.security.test.unit import BaseTestCase |
---|
16 | from ndg.security.server.wsgi.openid.provider.axinterface import ( |
---|
17 | AXInterfaceConfigError) |
---|
18 | from ndg.security.server.wsgi.openid.provider.axinterface.sqlalchemy_ax import ( |
---|
19 | SQLAlchemyAXInterface) |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | class SQLAlchemyAXInterfaceTestCase(BaseTestCase): |
---|
24 | def __init__(self, *arg, **kw): |
---|
25 | super(SQLAlchemyAXInterfaceTestCase, self).__init__(*arg, **kw) |
---|
26 | self.initDb() |
---|
27 | |
---|
28 | def test01InvalidQueryUsernameKey(self): |
---|
29 | interface = SQLAlchemyAXInterface() |
---|
30 | interface.connectionString = \ |
---|
31 | SQLAlchemyAXInterfaceTestCase.DB_CONNECTION_STR |
---|
32 | |
---|
33 | interface.sqlQuery = ("select firstname from users where username = " |
---|
34 | "'${invalidUsernameKey}'") |
---|
35 | |
---|
36 | axReq = FetchRequest() |
---|
37 | axResp = FetchResponse() |
---|
38 | |
---|
39 | authnCtx = { |
---|
40 | SQLAlchemyAXInterface.USERNAME_SESSION_KEYNAME: |
---|
41 | SQLAlchemyAXInterfaceTestCase.USERNAME |
---|
42 | } |
---|
43 | |
---|
44 | try: |
---|
45 | interface(axReq, axResp, None, authnCtx) |
---|
46 | |
---|
47 | except AXInterfaceConfigError: |
---|
48 | pass |
---|
49 | else: |
---|
50 | self.fail("Expected AXInterfaceConfigError exception") |
---|
51 | |
---|
52 | def test02(self): |
---|
53 | interface = SQLAlchemyAXInterface() |
---|
54 | interface.connectionString = \ |
---|
55 | SQLAlchemyAXInterfaceTestCase.DB_CONNECTION_STR |
---|
56 | |
---|
57 | interface.attributeNames = ('firstName', 'lastName', 'emailAddress') |
---|
58 | |
---|
59 | interface.sqlQuery = ("select firstname, lastname, emailAddress from " |
---|
60 | "users where username = '${username}'") |
---|
61 | |
---|
62 | axReq = FetchRequest() |
---|
63 | |
---|
64 | for typeURI in interface.attributeNames: |
---|
65 | axReq.add(AttrInfo(typeURI, required=True)) |
---|
66 | |
---|
67 | axResp = FetchResponse() |
---|
68 | |
---|
69 | authnCtx = { |
---|
70 | SQLAlchemyAXInterface.USERNAME_SESSION_KEYNAME: |
---|
71 | SQLAlchemyAXInterfaceTestCase.USERNAME |
---|
72 | } |
---|
73 | |
---|
74 | interface(axReq, axResp, None, authnCtx) |
---|
75 | axData = axResp.getExtensionArgs() |
---|
76 | self.assert_(len(axData.keys()) > 0) |
---|
77 | print(axData) |
---|
78 | |
---|