Beispiel #1
0
        public async Task GetCoursesTest()
        {
            var objA = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationODataTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Courses.Add(objA);
                });
            })
                                           );
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var resp = await client.GetStringAsync($"odata/v1/{nameof(Course)}s?$count=true");

            //Validate OData Result
            TestContext.WriteLine($"Server Reponse: {resp}");
            var envelope = JsonConvert.DeserializeObject <ODataEnvelope <Course> >(resp);

            Assert.AreEqual(objA.Title, envelope.Value.First().Title);
        }
Beispiel #2
0
        public async Task SaveCourseTest()
        {
            var item = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationTestStartup>());
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var resp = await client.PostAsJsonAsync($"api/v1/{nameof(Course)}s.json", item);

            _ = resp.EnsureSuccessStatusCode();
            var httpCourse = await DeserializeResponseAsync <Course>(resp);

            Assert.AreEqual("*****@*****.**", httpCourse.CreatedBy);

            var dbContext = srv.GetDbContext <DatabaseContext>();
            var dbCourses = await dbContext.Courses.ToListAsync();

            Assert.AreEqual(1, dbCourses.Count);
            var dbCourse = dbCourses.FirstOrDefault();

            Assert.IsNotNull(dbCourse);
            Assert.AreEqual(httpCourse.CreatedOnUtc, dbCourse.CreatedOnUtc);
        }
Beispiel #3
0
        public async Task GetSchoolsWithExpansionsTest()
        {
            var school = Factories.SchoolFactory();
            var course = Factories.CourseFactory();

            school.SchoolCourses.Add(Factories.SchoolCourseFactory(school, course));
            using var srv = new TestServer(TestHostBuilder <Startup, IntegrationODataTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Schools.Add(school);
                    _ = db.Courses.Add(course);
                    db.SchoolCourses.AddRange(school.SchoolCourses);
                });
            })
                                           );
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var resp = await client.GetStringAsync(
                $"odata/v1/{nameof(School)}s?$filter=id eq {school.Id}&$expand={nameof(school.SchoolCourses)},{nameof(school.StudentSchools)}");

            TestContext.WriteLine($"Server Reponse: {resp}");

            var envelope = JsonConvert.DeserializeObject <ODataEnvelope <School> >(resp);

            Assert.IsFalse(resp.ToLower().Contains("updatedby"));
            Assert.AreEqual(1, envelope.Value.First().SchoolCourses.Count);
            StringAssert.Contains(resp, school.Name);
        }
Beispiel #4
0
        public async Task GetGroupByCoursesTest()
        {
            var Course = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, IntegrationODataTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Courses.Add(Course);
                });
            })
                                           );
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());
            HttpResponseMessage resp = null;

            try
            {
                resp = await client.GetAsync($"odata/v1/{nameof(Course)}s?$apply=groupby(({nameof(Course.Num)},{nameof(Course.Title)}))");
            }
            catch (Exception) { }
            var body = await resp.Content.ReadAsStringAsync();

            TestContext.WriteLine($"Server Reponse: {body}");
            Assert.IsFalse(body.ToLower().Contains("updatedby"));
            StringAssert.Contains(body, Course.Num);
        }
Beispiel #5
0
        public async Task DeleteCoursesTest()
        {
            var mockPublisher = MockRedisStreamFactory <Course, DeletedEvent> .CreatePublisher();

            var Course = Factories.CourseFactory();
            var result = await new CoursesController().Delete(Course.Id, mockPublisher.Object)
                         as OkObjectResult;

            mockPublisher.Verify(t => t.PublishAsync(It.Is <Course>(t => t.Id == Course.Id)), Times.Once);
            Assert.AreEqual(200, result.StatusCode);
        }
Beispiel #6
0
        public async Task SaveCourseJsonReaderExceptionsTest()
        {
            var item = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationTestStartup>());
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var resp = await client.PostAsJsonAsync($"api/v1/{nameof(Course)}s.xml", item);

            _ = resp.EnsureSuccessStatusCode();
            _ = await DeserializeResponseAsync <Course>(resp);
        }
Beispiel #7
0
        public async Task SaveCourseCausesAuditInvalidUserExTest()
        {
            var item = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationTestStartup>());
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken(x =>
                                                         x.Remove(x.First(t => t.Type == JwtRegisteredClaimNames.Email))));

            var result = await client.PostAsJsonAsync($"api/v1/{nameof(Course)}s.json", item);

            Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode);
        }
Beispiel #8
0
        public async Task UpdateCourseTest()
        {
            var originalCourse = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Courses.Add(originalCourse);
                });
            }));
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var updatedCourse = JsonConvert.DeserializeObject <Course>(JsonConvert.SerializeObject(originalCourse));

            updatedCourse.Title = Guid.NewGuid().ToString()[..6];
Beispiel #9
0
        public async Task DeleteCoursesTest()
        {
            var mockPublisher = MockRedisStreamFactory <Course, DeletedEvent> .CreatePublisher();

            var item = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationEventsTestStartup>().ConfigureServices(x =>
            {
                _ = x.AddSingleton(mockPublisher.Object);
            }));

            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var resp = await client.DeleteAsync($"api/v1/{nameof(Course)}s.json?id={item.Id}");

            _ = resp.EnsureSuccessStatusCode();
            mockPublisher.Verify(t => t.PublishAsync(It.Is <Course>(t => t.Id == item.Id)), Times.Once);
        }
Beispiel #10
0
        public async Task UpdateCourseCausesAuditInvalidUserExTest()
        {
            var originalCourse = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Courses.Add(originalCourse);
                });
            }));
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken(x =>
                                                         x.Remove(x.First(t => t.Type == JwtRegisteredClaimNames.Email))));

            var updatedCourse = JsonConvert.DeserializeObject <Course>(JsonConvert.SerializeObject(originalCourse));

            updatedCourse.Num = Guid.NewGuid().ToString()[..6];
Beispiel #11
0
        public async Task GetCourseTestAsync()
        {
            var item = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, UnigrationTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Courses.Add(item);
                });
            }));
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken(new[] { new Claim("MyTestClaim", Guid.NewGuid().ToString()) }));
            //Get
            var resp = await client.GetAsync($"api/v1/{nameof(Course)}s.json?id={item.Id}");

            var httpCourse = await DeserializeResponseAsync <Course>(resp);

            Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);
            Assert.AreEqual(item.Title, httpCourse.Title);
        }
Beispiel #12
0
        public async Task GetGroupByCoursesTestWithAggregations()
        {
            var Course = Factories.CourseFactory();

            using var srv = new TestServer(TestHostBuilder <Startup, IntegrationODataTestStartup>()
                                           .ConfigureTestServices(x =>
            {
                ExecuteOnContext <DatabaseContext>(x, db =>
                {
                    _ = db.Courses.Add(Course);
                });
            })
                                           );
            var client = srv.CreateClient();

            GenerateAuthHeader(client, GenerateTestToken());

            var resp = await client.GetStringAsync($"odata/v1/{nameof(Course)}s?$apply=aggregate(id with countdistinct as total)");

            TestContext.WriteLine($"Server Reponse: {resp}");
            Assert.IsFalse(resp.ToLower().Contains("updatedby"));
            StringAssert.Contains(resp, "total");
        }