CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataPortal/AuthorizeDataPortalTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AuthorizeDataPortalTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Configuration;
13using Csla.Server;
14using Csla.TestHelpers;
16using System.Threading.Tasks;
17
18#if !NUNIT && !ANDROID
19using Microsoft.VisualStudio.TestTools.UnitTesting;
20#elif !ANDROID
21using NUnit.Framework;
22using TestClass = NUnit.Framework.TestFixtureAttribute;
23using TestInitialize = NUnit.Framework.SetUpAttribute;
24using TestCleanup = NUnit.Framework.TearDownAttribute;
25using TestMethod = NUnit.Framework.TestAttribute;
26#endif
27
28
30{
31 [TestClass]
33 {
34 private static TestDIContext _testDIContext;
35
36 #region SetUp
37
39 public static void ClassInitialize(TestContext context)
40 {
41 _testDIContext = TestDIContextFactory.CreateContext(options =>
42 {
43 options.Services.AddTransient<TestableDataPortal>();
44 options.DataPortal(
45 dp => dp.AddServerSideDataPortal(
46 config => config.RegisterAuthorizerProvider<AuthorizeDataPortalStub>())
47 );
48 });
49 }
50
51 [TestInitialize]
52 public void Setup()
53 {
55
56 //Csla.Configuration.ConfigurationManager.AppSettings["CslaAuthorizationProvider"] =
57 // "Csla.Testing.Business.DataPortal.AuthorizeDataPortalStub, Csla.Testing.Business";
58
59 }
60
61 #endregion
62
63 #region constructor(Type authProviderType) tests
64
65 // TODO: I don't think the new config mechanism supports setting this to an invalid value?
66 //[TestMethod]
67 //[ExpectedException(typeof(ArgumentNullException))]
68 //public void IfAuthProviderTypeIsNull_ThenThrow_ArgumentNullException()
69 //{
70 // Type authProviderType = null;
71 // new TestableDataPortal(authProviderType);
72 //}
73
74 // TODO: I don't think the new config mechanism supports setting this to an invalid value?
75 //[TestMethod]
76 //[ExpectedException(typeof(ArgumentException))]
77 //public void IfAuthProviderTypeDoesNotImplement_IAuthorizeDataPortal_ThenThrow_ArgumentException()
78 //{
79 // Type authProviderTypeNotImplementing_IAUthorizeDataPortal = typeof(object);
80 // new TestableDataPortal(authProviderTypeNotImplementing_IAUthorizeDataPortal);
81 //}
82
83 [TestMethod]
85 {
86 var dp = _testDIContext.ServiceProvider.GetRequiredService<TestableDataPortal>();
87
88 Assert.IsTrue(dp.AuthProviderType == typeof(AuthorizeDataPortalStub));//_authorizer field is set to correct value;
89 }
90
91 #endregion
92
93 #region constructur(string cslaAuthorizationProvider) tests
94
95 // TODO: I don't think this is a valid test now that config method has changed?
96 //[TestMethod]
97 //[ExpectedException(typeof(ArgumentNullException))]
98 //public void IfCslaAuthorizationProviderAppSettingName_IsNull_ThenThrow_ArgumentNullException()
99 //{
100 // const string appSettingName = null;
101 // new TestableDataPortal(appSettingName);
102 //}
103
104 // TODO: I don't think this is a valid test now that config method has changed?
105 //[TestMethod]
106 //public void IfCslaAuthorizationProviderAppSetting_DoesNotExist_ThenUse_NullAuthorizationProvider()
107 //{
108 // var dp = new TestableDataPortal("NonExistentAppSetting");
109 // Assert.IsTrue(dp.NullAuthorizerUsed);
110 //}
111
112 // TODO: I don't think this is a valid test now that config method has changed?
113 //[TestMethod]
114 //[ExpectedException(typeof(TypeLoadException))]
115 //public void IfCslaAuthorizationProviderAppSetting_HoldsInvalidType_ThenThrow_TypeLoadException()
116 //{
117 // ConfigurationManager.AppSettings["InvalidTypeName"] ="InvalidTypeName";
118 // var dp = new TestableDataPortal("InvalidTypeName");
119 //}
120
121 // TODO: I don't think this is a valid test now that config method has changed?
122 //[TestMethod]
123 //public void IfCslaAuthorizationProviderAppSetting_HoldsEmptyString_ThenUse_NullAuthorizationProvider()
124 //{
125 // ConfigurationManager.AppSettings["EmptyTypeName"] = string.Empty;
126 // var dp = new TestableDataPortal("EmptyTypeName");
127 //
128 // Assert.IsTrue(dp.NullAuthorizerUsed);
129 //}
130
131 // TODO: I don't think this is a valid test now that config method has changed?
132 //[TestMethod]
133 //public void IfCslaAuthorizationProviderAppSetting_HoldsValidType_Then_authorizerFieldShouldHoldThatType()
134 //{
135 // ConfigurationManager.AppSettings["ValidTypeNameSetting"] = "Csla.Testing.Business.DataPortal.AuthorizeDataPortalStub, Csla.Testing.Business";
136 // var dp = new TestableDataPortal("ValidTypeNameSetting");
137 //
138 // Assert.IsTrue(typeof(AuthorizeDataPortalStub)==dp.AuthProviderType);
139 //}
140
141 #endregion
142
143 #region default constructor() tests
144
145 // TODO: I don't think this is a valid test now that config method has changed?
146 //[TestMethod]
147 //public void IfAuthorizationProvider_SetInConfigFile_DataPortal_Instantiates_AuthorizationProviderType()
148 //{
149 // //Following is set in App.Config
150 // //ConfigurationManager.AppSettings["CslaAuthorizationProvider"] = "Csla.Test.Silverlight.DataPortal.AuthorizeDataPortalStub, Csla.Test";
151 // TestableDataPortal dp = new TestableDataPortal();
152 //
153 // Assert.IsTrue(typeof(AuthorizeDataPortalStub) == dp.AuthProviderType);
154 //}
155
156 #endregion
157
158 #region Authorize() tests
159
160 [TestMethod]
162 {
163 var applicationContext = _testDIContext.CreateTestApplicationContext();
164 var dp = _testDIContext.ServiceProvider.GetRequiredService<TestableDataPortal>();
165 await dp.Create(typeof(TestBO), null, new DataPortalContext(applicationContext, applicationContext.Principal, false), true);
166
167 var result = (AuthorizeDataPortalStub)dp.AuthProvider;
168
169 Assert.IsNotNull(result, "AuthorizeDataPortalStub not accessible");
170 Assert.AreEqual(typeof(TestBO), result.ClientRequest?.ObjectType);
171 Assert.AreEqual(DataPortalOperations.Create, result.ClientRequest.Operation);
172 }
173
174 [TestMethod]
176 {
177 var applicationContext = _testDIContext.CreateTestApplicationContext();
178 var dp = _testDIContext.ServiceProvider.GetRequiredService<TestableDataPortal>();
179 await dp.Fetch(typeof(TestBO), null, new DataPortalContext(applicationContext, applicationContext.Principal, false), true);
180
181 var result = (AuthorizeDataPortalStub)dp.AuthProvider;
182
183 Assert.IsNotNull(result, "AuthorizeDataPortalStub not accessible");
184 Assert.AreEqual(typeof(TestBO), result.ClientRequest?.ObjectType);
185 Assert.AreEqual(DataPortalOperations.Fetch, result.ClientRequest.Operation);
186 }
187
188 [TestMethod]
190 {
191 var applicationContext = _testDIContext.CreateTestApplicationContext();
192 var dp = _testDIContext.ServiceProvider.GetRequiredService<TestableDataPortal>();
193 await dp.Update(new TestBO(), new DataPortalContext(applicationContext, applicationContext.Principal, false), true);
194
195
196 var result = (AuthorizeDataPortalStub)dp.AuthProvider;
197
198 Assert.IsNotNull(result, "AuthorizeDataPortalStub not accessible");
199 Assert.AreEqual(typeof(TestBO), result.ClientRequest?.ObjectType);
200 Assert.AreEqual(DataPortalOperations.Update, result.ClientRequest.Operation);
201 }
202
203 [TestMethod]
205 {
206 var applicationContext = _testDIContext.CreateTestApplicationContext();
207 var dp = _testDIContext.ServiceProvider.GetRequiredService<TestableDataPortal>();
208 await dp.Delete(typeof(TestBO), new object(), new DataPortalContext(applicationContext, applicationContext.Principal, false), true);
209
210 var result = (AuthorizeDataPortalStub)dp.AuthProvider;
211
212 Assert.IsNotNull(result, "AuthorizeDataPortalStub not accessible");
213 Assert.AreEqual(typeof(TestBO), result.ClientRequest?.ObjectType);
214 Assert.AreEqual(DataPortalOperations.Delete, result.ClientRequest.Operation);
215 }
216
217 #endregion
218 }
219
220
221}
Csla.Server.DataPortalContext DataPortalContext
DataPortal(ApplicationContext applicationContext, IDashboard dashboard, CslaOptions options, IAuthorizeDataPortal authorizer, InterceptorManager interceptors, IObjectFactoryLoader factoryLoader, IDataPortalActivator activator, IDataPortalExceptionInspector exceptionInspector, DataPortalExceptionHandler exceptionHandler)
Creates an instance of the type.
Definition: DataPortal.cs:54
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
Definition: DataPortal.cs:214
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
Definition: DataPortal.cs:308
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
Definition: DataPortal.cs:117
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
Definition: DataPortal.cs:441
Type to carry context information for DI in unit tests
IServiceProvider ServiceProvider
The service provider used to perform DI operations
Basically this test class exposes protected DataPortal constructor overloads to the Unit Testing Syst...
DataPortalOperations
List of data portal operations.