Monday, May 23, 2011

Rails 3 - Testing Authentication (format html and xml)

Hey guys,

This is a brief post showing you some functional test examples that test site authentication. I'm testing both format:html (via sessions), and format:xml (via HTTP Basic Auth).


Format HTML tests (session authorization):

test "should show user's account when logged in" do
    get :account, { :id => @user.to_param }, { :user => @user.to_param }
    assert_response :success
end

test "shouldn't show user's account when not logged in" do
    get :account, { :id => @user.to_param }
    assert_redirected_to new_session_path
end

test "shouldn't get edit as logged in as different user" do
    get :edit, { :id => @other_user.to_param }, { :user => @user.to_param }
    assert_redirected_to user_path(@other_user)
end


Format XML Tests (http_auth authorization):

test "shouldn't show account (xml) without http_auth provided" do
    get :account, { :format => 'xml', :id => @user.to_param }, { :user => @user.to_param }
    assert_response :unauthorized
end

test "should show account (xml) with http_auth provided"  do
    @request.env['HTTP_AUTHORIZATION'] = encode_credentials(@user.user_name, password_for(@user))
    get :account, { :format => 'xml', :id => @user.to_param }
    assert_response :success
end

test "shouldn't show account (xml) with bad http_auth provided"  do
    @request.env['HTTP_AUTHORIZATION'] = encode_credentials('foo', 'bar')
    get :account, { :format => 'xml', :id => @user.to_param }
    assert_response :unauthorized
  end

N.B.
Assumes that in your test helper files you have a password_for(user) method.

Assumes the following test setup:
setup do
    @user = users(:joe)
    @other_user = users(:jane)
end

Saturday, March 6, 2010

vimrc generator

Hey guys and gals,

I recently got sick of trawling through my old emails to find a .vimrc file a work colleague had previously sent me, so I decided to write a simple .vimrc generator based on the vimrc file I was working with.

The vimrc generator can be found at http://vimrcgenerator.appspot.com

I wrote the vimrc generator for appengine. I will probably add the source code to github in the near future.
Enjoy,

Sunday, January 24, 2010

Simple fix for the looping "Dropbox daemon not installed!" problem

I performed an Ubuntu update the other day, and my dropbox stopped working.

It kept asking me to install the dropbox daemon, even though I had already installed it.

I fixed this problem by removing the hidden dropbox folders in my home directory, and then reinstalling the dropbox package.


Short answer:

Remove the two hidden dropbox folders (~/.dropbox and ~/.dropbox-dist) from your home directory, and try running dropbox again.


Long answer:

Step 1 - Remove the two hidden dropbox folders:

  • rm -rfd ~/.dropbox-dist/
  • rm -rfd ~/.dropbox/

Step 2 - Reinstall dropbox:

Step 3 - Open dropbox:

  • Open dropbox, dropbox is usually found in your applications drop-down under internet.

Step 4 - Install daemon and configure:

  • When you run dropbox, it will install the dropbox daemon. Once it has completed that step, it should ask you to log out and in again. Log out, then log back in.
  • Once you have logged back in, re-run dropbox. This time when you run dropbox, it will ask you for your login details. Provide your login details.
  • Dropbox will now ask where you want to sync your dropbox to. Select the same folder as you previously used.

Step 5 - Finished

  • Dropbox should now be working again!

Wednesday, March 4, 2009

Android application release stats

Hey guys and gals,

I have a treat for those of you who are curious about what the uptake rate is for Android apps. I will be sharing my uptake stats via a google spreadsheet.

N.B. These are of course the stats for my apps, meaning that they will not reflect all apps. Also, so far, my apps are for "niche" markets. The release stats would likely be very different for an app such as "Lord of the farts"; Lol, "Lord of the farts".

Saturday, February 28, 2009

The most useful piece of code I have written for Android so far

The following code generates a new tab whenever there is an error and prints the error inside the tab. AWESOME. It is quick and easy, and provides you with immediate feedback when something goes wrong.

} catch (Exception e) {
tabHost.addTab(tabHost.newTabSpec("ERROR")
.setIndicator("ERROR")
.setContent(R.id.errorText)
);
TextView errorText = (TextView) findViewById(R.id.errorText);
StringBuilder error = new StringBuilder();
error.append("Error: " + e.getMessage() + "\n");
for(StackTraceElement te : e.getStackTrace()) {
error.append("f: " + te.getFileName() + ", m: " + te.getMethodName() + ", l: " + te.getLineNumber() + "\n");
}
errorText.setText(error.toString());
}


N.B. I am using a TabActivity, the tabHost variable is the result of this.getTabHost().
R.id.errorText refers to a TextView I have in my layout xml.

Ohmage taking shape

Hey people,

the following is another screen shot. It shows how Ohmage is taking shape.
Ohmage can now go from text to color, as well as color to text.

In the screen you can see the auto-completion functionality in use.


Friday, February 27, 2009

Ohmage

Hey guys and gals,

I've been spending a little bit of time working with Android lately. The following is a sneak peek at an app I'm working on. I hope to have it added to the Android market place by the end of this weekend (free app). The app's name is Ohmage, however for stuff and giggles, I may change the name to Pure Ohmage. I should mention that I hope to also have several other features added before I release Ohmage.

These features include:
1. A "generate colors" based on resistance value (ohms).
2. A lookup formula/equation list. This list will initially be static.
3. A multi tab view to separate the ohmage view from the formula/equation list view.

N.B. You may notice the resistor image is very coder-art-ish. The image will be updated before I release Ohmage.






Update:

While I do only support 4 band resistors, it will be fairly simple to add support for 5 and 6 band resistors.

For anyone wanting to know what the learning curve for Android is like, the following is my take on learning Android. I have been working with Android for about 10 hours now. Android programming with the Eclipse plugin is... pleasant :D. Android programs are written in Java and the event system is fairly similar to Java Swing. A lot of the heavy lifting is performed by the Android View, Activity and Adapter classes.
The Api Demos included with the SDK are the holy grail for learning Android. They provide excellent examples and clearly show how to perform your layout in xml, rather than in the raw code.
If you are considering learning Android, I highly recommend making use of the eclipse plugin and the Api Demos.