00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "MoodleTests.h"
00010 #import "Moodle.h"
00011
00012 @implementation MoodleTests
00013
00014 - (void) setUp
00015 {
00016 moodle = [[Moodle alloc] init];
00017 [moodle sync];
00018 }
00019
00020 - (void) tearDown
00021 {
00022 [moodle release];
00023 }
00024
00025 - (void) testLogin
00026 {
00027 [moodle login:@"admin" password:@"1234"];
00028 if (!moodle.loggedIn) {
00029 STFail(@"Unable to log in");
00030 }
00031 }
00032
00033 - (void) testWrongLogin
00034 {
00035 [moodle login:@"admin" password:@"123"];
00036 if (moodle.loggedIn) {
00037 STFail(@"Cannot detect incorrect username and password.");
00038 }
00039 }
00040
00041 - (void) testLogout
00042 {
00043 [self testLogin];
00044 [moodle logout];
00045 if (moodle.loggedIn) {
00046 STFail(@"Cannot log out");
00047 }
00048 }
00049
00050 - (void) testGetCourses
00051 {
00052 [self testLogin];
00053 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00054 initWithObject:self
00055 finished:@selector(getCoursesFinished:)
00056 failed:@selector(failed:error:)];
00057 [moodle getCourses:w];
00058 }
00059
00060 - (void)getCoursesFinished:(RMResponse*) response
00061 {
00062 id r = [[response responseString] JSONValue];
00063 if (![r isKindOfClass:[NSDictionary class]]) {
00064 STFail(@"Not a dictionary");
00065 }
00066 r = [r valueForKey:@"courses"];
00067 if (nil == r || ![r isKindOfClass:[NSArray class]]) {
00068 STFail(@"Courses not recognized.");
00069 }
00070 }
00071
00072 - (void) testGetSingleCourse
00073 {
00074 [self testLogin];
00075 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00076 initWithObject:self
00077 finished:@selector(getCoursesFinished:)
00078 failed:@selector(failed:error:)];
00079 [moodle getCourse:1 delegate:w];
00080 }
00081
00082 - (void) testGetMyCourses
00083 {
00084 [self testLogin];
00085 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00086 initWithObject:self
00087 finished:@selector(getCoursesFinished:)
00088 failed:@selector(failed:error:)];
00089 [moodle getMyCourses:w];
00090 }
00091
00092 - (void) testGetResources
00093 {
00094 [self testLogin];
00095 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00096 initWithObject:self
00097 finished:@selector(getResourcesFinished:)
00098 failed:@selector(failed:error:)];
00099 [moodle getResources:1 delegate:w];
00100 }
00101
00102 - (void)getResourcesFinished:(RMResponse*) response
00103 {
00104 id r = [[response responseString] JSONValue];
00105 if (![r isKindOfClass:[NSDictionary class]]) {
00106 STFail(@"Not a dictionary");
00107 }
00108 r = [r valueForKey:@"resources"];
00109 if (nil == r || ![r isKindOfClass:[NSArray class]]) {
00110 STFail(@"Resources not recognized.");
00111 }
00112 }
00113
00114 - (void) testGetTeachers
00115 {
00116 [self testLogin];
00117 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00118 initWithObject:self
00119 finished:@selector(getUsersFinished:)
00120 failed:@selector(failed:error:)];
00121 [moodle getTeachers:1 delegate:w];
00122 }
00123
00124 - (void)getUsersFinished:(RMResponse*) response
00125 {
00126 id r = [[response responseString] JSONValue];
00127 if (![r isKindOfClass:[NSDictionary class]]) {
00128 STFail(@"Not a dictionary");
00129 }
00130 r = [r valueForKey:@"users"];
00131 if (nil == r || ![r isKindOfClass:[NSArray class]]) {
00132 STFail(@"Users not recognized.");
00133 }
00134 }
00135
00136 - (void) testGetStudents
00137 {
00138 [self testLogin];
00139 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00140 initWithObject:self
00141 finished:@selector(getUsersFinished:)
00142 failed:@selector(failed:error:)];
00143 [moodle getStudents:1 delegate:w];
00144 }
00145
00146 - (void) testGetActivities
00147 {
00148 [self testLogin];
00149 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00150 initWithObject:self
00151 finished:@selector(getActivitiesFinished:)
00152 failed:@selector(failed:error:)];
00153 [moodle getActivities:1 delegate:w];
00154 }
00155
00156 - (void)getActivitiesFinished:(RMResponse*) response
00157 {
00158 id r = [[response responseString] JSONValue];
00159 if (![r isKindOfClass:[NSDictionary class]]) {
00160 STFail(@"Not a dictionary");
00161 }
00162 r = [r valueForKey:@"activities"];
00163 if (nil == r || ![r isKindOfClass:[NSArray class]]) {
00164 STFail(@"Activities not recognized.");
00165 }
00166 }
00167
00168 - (void) testGetLastChanges
00169 {
00170 [self testLogin];
00171 RMResultDelegateWrapper *w = [[RMResultDelegateWrapper alloc]
00172 initWithObject:self
00173 finished:@selector(getChangesFinished:)
00174 failed:@selector(failed:error:)];
00175 [moodle getLastChanges:1 delegate:w];
00176 }
00177
00178 - (void)getChangesFinished:(RMResponse*) response
00179 {
00180 id r = [[response responseString] JSONValue];
00181 if (![r isKindOfClass:[NSDictionary class]]) {
00182 STFail(@"Not a dictionary");
00183 }
00184 r = [r valueForKey:@"changes"];
00185 if (nil == r || ![r isKindOfClass:[NSArray class]]) {
00186 STFail(@"Changes not recognized.");
00187 }
00188 }
00189
00190
00191 #pragma mark support functions
00192 - (void)failed:(RMResponse*) response error:(NSError*) error
00193 {
00194 STFail([error localizedFailureReason]);
00195 }
00196
00197 @end