www.davidarodriguez.com

I drink coffee and program. Meh.

Navigation Menu

ClaimsPrincipalPermission and unit testing

Posted by on Oct 22, 2014 in Blog, Unit Testing | 0 comments

Recently, I was trying to take a stab at unit testing using NUnit and FakeItEasy. My first unit test started with the following code:

 

All I really wanted to do was to see if the method does return a instance of ActionResult. Upon executing the unit test, I came across this error:

A first chance exception of type ‘System.InvalidOperationException’ occurred in System.IdentityModel.dll

Additional information: ID7027: Could not load the identity configuration because no <system.identityModel> configuration section was found.

The error specifically pointed out that this was occurring at the ClaimsPrincipalPermission attribute.

 

ClaimsPrincipalPermission InvalidOperationException occurred.

ClaimsPrincipalPermission InvalidOperationException occurred.

After some research, I came across that the reason that this was occurring was that ClaimsPrincipalPermission becomes invoked at the CLR level, which in turn it affects unit testing.

This approach typically gets in your way, e.g. ClaimsPrincipalPermission gets invoked directly by the CLR, which means it will also run in unit tests, and unhandled exceptions short-circuit your processing pipeline. http://leastprivilege.com/2012/10/26/using-claims-based-authorization-in-mvc-and-web-api/

Looking at StackOverflow, Dominick suggested you should create a custom ClaimsAuthorizationManager to get around this. You would have your custom ClaimsAuthorizationManager to always return true.

Use a custom ClaimsAuthorizationManager that always returns true for the unit tests.

PrincipalPermissions in general are not very unit testing friendly. http://stackoverflow.com/questions/15429811/claimsprincipalpermission-and-unit-testing/15436366#15436366

So, I did as he suggested. In my AuthorizationManager class, with my current production AuthorizationManager, I created a unit testing version as so:

Lastly, within my unit testing project I added the following to the app.config file:

When all this was setup, I was able to run my unit test successfully.

Hope this helps you.

Read More

Remove unused Using statements in your C#’s code behind

Posted by on Sep 26, 2014 in Visual Studio 2013 | 0 comments

I came across something pretty useful while learning some in and outs of ReSharper. I do not believe this is ReSharper specific, but built into Visual Studio itself. Code can get messy, especially the Using statements in your code behind. I thought ReSharper had some tool to clear this up for me, however, at first glance I did not see anything (maybe you know where it is?). As I right clicked my code behind’s page, the contextual menu popped up as normal, then I noticed “Organize Usings“. Oh, now this is interesting. Could this possibly be what I was looking for? As you can see, it provides the following:

  • Remove Unused Usings
  • Sort Usings
  • Remove and Sort

Yep, “Remove and Sort” is just the thing I was looking for. Take a look at the following screenshot.

Organizing Using Directives

Organizing Using Directives

Read More

Linqpad: Using Linqpad as a C# scratchpad

Posted by on Sep 15, 2014 in Blog, Linq | 0 comments

LINQPad is a excellent tool when writing LINQ queries against your database or some form of repository. Not only is it good for writing queries, you could use LINQPad as a scratchpad. Recently, I was tasked into writing a parser for our outbound scheduler service. This service would look into the database and select all current outgoing packets. Once retrieved, the dataset was cast to its respective datatype.  With these packets we commands are embedded into a commands property delimited by semi-colons. Previously, we just put a 1:1 command to command property, so I needed a way to test in quick manner if the parser would work. The parser is pretty simple. As you see I created a string array that held these commands. I would split them based on the character ‘;’. Then I would loop through these commands and output them to the console. I needed to test two versions of this. One with only one command, and another with multiple commands. I supposed I could have written a unit test as well, however, I wanted to illustrate you could use LINQPad for simple logic as this, as well, complex logic.

 

LINQPad  - C# Scratchpad Demo

 

 

Recommended Reading:



List Price: $39.99 USD
New From: $24.70 USD In Stock
Used from: $49.34 USD In Stock

Read More

Does my solution use this dll?

Posted by on Jul 2, 2014 in Visual Studio 2013 | 0 comments

Been a while, but here you go. I am currently working on an application that uses Google’s Maps API. Or current project uses older Subgurim and Reimer dlls that take advantage of the API. But did we actually use both set of dlls in order to use Google’s Map API? A quick way to figure this out is to open your object browser (you can open the object browser from the View menu), find the dll in question, right click it, and select ‘Find Usages’. By doing so, it will show you all occurrences of usage of the dll in question. Comes to show, we do not use Subgurim, but we do Reimer. Time to update that dll and move on with this project.

 

Visual Studio - Find Usages

Visual Studio – Find Usages

 

 

Read More

QueryString has its limits, try passing data via IRequiresSessionState or IReadOnlySessionState

Posted by on Mar 11, 2014 in Asp.Net, Blog | 0 comments

Oh hey I have a blog, totally forgot about that. Its been busy here lately, but that is no excuse to neglect my blog. So in light of this, I am going to post something I used recently when passing data to a generic handler via query string was just not cutting it. Passing data via query string has limitation in the length of data you can send. Here is a in depth post on these limitations http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string

I’ll sum up these limitations briefly:

  • Internet Explorer
    • 2,038 character limit, boo hiss boo
  • Firefox
    • 65,536, but could work for more
  • Safari
    • 80,000 character limit, but could work for more
  • Opera
    • 190,000 character limit, but could work for more
  • Apache
    • 4,000 character limit, but could work for more
  • IIS – Internet Information Server
    • 16,384, but could work for more
    • IE accepts less, but the server can accept more. Huh?

Since we have to make our web app work with IE there is no choice but to somehow shoe horn our parameters to be passed to generic methods. Instead of using query strings, another approach would be to use the IRequireSessionState or IReadOnlySessionState interface. For my generic handler I used the IReadOnlySessionState interface. In the code behind calling the generic handler, I stuffed the data I needed into session variables, Session["Key"] = value;

For the generic handler, I had it implement the IReadOnlySessionState. I didn’t want it to do anything, but to read those session variables.

Here’s what I did:

So, I can practically send any type of object I wanted to my handlers and processed that data according to business rules. Well, till next time.

References:

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.ireadonlysessionstate(v=vs.110).aspx

Read More

Breakpoints and Tracepoints: What are they?

Posted by on Jan 24, 2014 in Blog, Visual Studio 2013 | 0 comments

So, your in the process of making an app, not just any app, but the most electrifying app that has ever been known to mankind. Before shipping your app, there are a few things you have check on. Is your app intuitive? Does it make sense placing a button on the main page when a swipe gesture could be better? How pretty is your app? We all know apps that look pretty sell well, for instance, take a look at the iPhone Hippies (hipsters and hippies who go ooh and awe for anything that Apple makes). How secure is your app? Is it NSA  proof? Apps dealing with sensitive data needs to be handled accordingly, and by that, I don’t mean by the NSA. Does your app fit the requirements of its demographics? Think of who would be using your app and make all needed features to accommodate that user and keep them happy. A few other things to keep in mind would be the speed of the app (is there a need for speed?), its localization (location based services), internationalization (the more places your app resides the more money you make – North America, South America, Europe and so on), lastly how stable is your app? You won’t know how stable it is unless you test the hell out of it.

What are your options in testing your app then? There are a  couple ways of going about this. One is using Unit Testing and another would be integration testing. The most common form of testing is integration testing. If your a developer more than likely you already have done integration testing.

Integration testing in a nut shell are tests that are not fast nor consistent and heavily depend on other units of the system in order to retrieve an end result. Its all integrated. One part does not work without the help of another. You can think of a car engine as a type of integration. It relies heavily on subsystem after subsystem in order to get it to work. Parts relying on other parts to produce an end result. The car moving would be that end result. Realistically, you would want to perform both types of testing (unit testing and integration testing). Its a lot of work, but the end result is worth it. This post will lead to many other debugging posts using Visual Studio’s debugger and later on to unit testing.

So for starters, let us discuss what a breakpoint and tracepoint is. A brief introduction of both is that a breakpoint tells the debugger, “Hey man, you might want to check this out, line 231. It looks fishy.” When the debugger acknowledges the breakpoint it pauses execution. At that point you can view all local variables in that method, the stack trace, outputs and do some inspection at that point of execution. This pause that the breakpoint initiates is called the break mode.

Tracepoints is the new kid on the block. A tracepoint is a breakpoint, but with hipper clothes and a swagging haircut. The tracepoint sort of looks like Vanilla Ice – To the Extreme, but much more level headed. The tracepoint lets you associate custom actions to the breakpoint. It tells the breakpoint “Alright stop! Collaborate and listen, Ice is back with his brand new invention…”. And all the other breakpoints are like “Ice Ice Baby, too cold, too cold…”. When a tracepoint is hit, the debugger performs whatever action(s) the tracepoint defined, including breaking program execution. I use tracepoints to print out messages of variables to the Output window as execution happens. There are times where I just get tired of stepping through code so I let the tracepoint do the work. Here’s an example of what setting up a tracepoint looks like. You would want to right click, in the source code, where you want to set the tracepoint at, select Breakpoint->Insert Tracepoint.

1-24-2014 3-07-41 PM

1-24-2014 3-05-53 PM

In future posts I’ll discuss what types of breakpoints/tracepoints there are. This is just serving as a little primer/introduction to both. Till next time.

 

References:

http://msdn.microsoft.com/en-us/library/232dxah7(v=vs.90).aspx

Recommended Reading:


Advanced Windows Debugging (Kindle Edition)

By (author): Mario Hewardt, Daniel Pravat

Kindle Edition: Check Amazon for Pricing Digital Only



Kindle Edition: Check Amazon for Pricing Digital Only

Read More
Page 1 of 1512345...10...Last »