public async Task AppPublishedGetPutCountNotificationTest()
        {
            // get the app handle
            string appHandle = ManageAppsUtils.GetAppHandle(TestConstants.EnvironmentName);

            if (appHandle == null)
            {
                // fail the test
                Assert.Fail("Failed to lookup appHandle");
            }

            var nt = new NotificationsTests();
            await nt.GetPutCountNotificationTestHelper(true, appHandle);
        }
        public async Task AppPublishedLikeCommentReplyDeleteTest()
        {
            // get the app handle
            string appHandle = ManageAppsUtils.GetAppHandle(TestConstants.EnvironmentName);

            if (appHandle == null)
            {
                // fail the test
                Assert.Fail("Failed to lookup appHandle");
            }

            var lt = new LikeTests();
            await lt.LikeCommentReplyDeleteTestHelper(true, appHandle);
        }
        public async Task AppPublishedPinUnpinTest()
        {
            // get the app handle
            string appHandle = ManageAppsUtils.GetAppHandle(TestConstants.EnvironmentName);

            if (appHandle == null)
            {
                // fail the test
                Assert.Fail("Failed to lookup appHandle");
            }

            var pt = new PinTests();
            await pt.PinUnPinDeletePinTestHelper(true, appHandle);
        }
        public async Task CreateVerifyDeleteNamedTopicTest()
        {
            HttpOperationResponse <object> deleteUserOperationResponse      = null;
            HttpOperationResponse <object> deleteTopicOperationResponse     = null;
            HttpOperationResponse <object> deleteTopicNameOperationResponse = null;
            DeleteTopicNameRequest         deleteTopicNameReq = new DeleteTopicNameRequest(publisherType: PublisherType.App);
            int endIndex = TestConstants.ConfigFileName.IndexOf(".");

            this.environment = TestConstants.ConfigFileName.Substring(0, endIndex);

            // create a user
            SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl);
            var t = await this.CreateUserForTest(client);

            PostUserResponse postUserResponse = t.Item1;
            string           auth             = t.Item2;
            string           userHandle       = postUserResponse.UserHandle;

            string appHandle = ManageAppsUtils.GetAppHandle(this.environment);

            if (appHandle == null)
            {
                // delete the user and fail the test
                deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to lookup appHandle");
            }

            // add user as admin
            bool added = ManageAppsUtils.AddAdmin(this.environment, appHandle, userHandle);

            if (!added)
            {
                // delete the user and fail the test
                deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to set user as administrator");
            }

            // create a topic
            string           topicTitle       = "Test topic for named topics";
            string           topicText        = "This sure is a fine topic.";
            BlobType         blobType         = BlobType.Image;
            string           blobHandle       = "http://myBlobHandle/";
            string           language         = "en-US";
            string           group            = "mygroup";
            PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.User, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, language: language, group: group);
            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth);

            // if create topic was a success, grab the topic handle
            string topicHandle = string.Empty;

            if (postTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                topicHandle = postTopicOperationResponse.Body.TopicHandle;
            }
            else
            {
                // otherwise, delete the admin, the user and fail the test
                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Create topic failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            string topicName = "UnitTestTopicName";

            // create a topic name
            PostTopicNameRequest           postTopicNameReq = new PostTopicNameRequest(publisherType: PublisherType.App, topicName: topicName, topicHandle: topicHandle);
            HttpOperationResponse <object> postTopicNameOperationResponse = await client.Topics.PostTopicNameWithHttpMessagesAsync(request : postTopicNameReq, authorization : auth);

            // if creating the topic name fails, delete the topic, the user, and fail the test
            if (!postTopicNameOperationResponse.Response.IsSuccessStatusCode)
            {
                deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Create topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            // get the topic name
            HttpOperationResponse <GetTopicByNameResponse> getTopicNameResponse = await client.Topics.GetTopicByNameWithHttpMessagesAsync(topicName : topicName, publisherType : PublisherType.App, authorization : auth);

            if (!getTopicNameResponse.Response.IsSuccessStatusCode)
            {
                // if get topic name fails, cleanup: delete the topic name, the admin, the user, and fail the test
                deleteTopicNameOperationResponse = await client.Topics.DeleteTopicNameWithHttpMessagesAsync(request : deleteTopicNameReq, authorization : auth, topicName : topicName);

                deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("get topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            // delete the topic name we just created
            deleteTopicNameOperationResponse = await client.Topics.DeleteTopicNameWithHttpMessagesAsync(request : deleteTopicNameReq, authorization : auth, topicName : topicName);

            // if deleting the topic name fails, delete the topic, the admin, the user, and fail the test
            if (!deleteTopicNameOperationResponse.Response.IsSuccessStatusCode)
            {
                deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Delete topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            // do standard cleanup : delete the topic, delete the admin, and then delete the user
            deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

            ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
            deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

            // if we reach here, the test was successful.
            return;
        }
        public async Task CreateUpdateDeleteNamedTopicTest()
        {
            var deleteRequest = new DeleteTopicNameRequest()
            {
                PublisherType = PublisherType.App
            };
            int endIndex = TestConstants.ConfigFileName.IndexOf(".");

            this.environment = TestConstants.ConfigFileName.Substring(0, endIndex);

            // create a user
            SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl);
            var t = await this.CreateUserForTest(client);

            PostUserResponse postUserResponse = t.Item1;
            string           auth             = t.Item2;
            string           userHandle       = postUserResponse.UserHandle;

            // get the app handle
            string appHandle = ManageAppsUtils.GetAppHandle(this.environment);

            if (appHandle == null)
            {
                // delete the user and fail the test
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to lookup appHandle");
            }

            // add user as admin
            bool added = ManageAppsUtils.AddAdmin(this.environment, appHandle, userHandle);

            if (!added)
            {
                // delete the user and fail the test
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to set user as administrator");
            }

            // create a topic
            string           topicTitle       = "Test topic for named topics";
            string           topicText        = "This sure is a fine topic.";
            BlobType         blobType         = BlobType.Image;
            string           blobHandle       = "http://myBlobHandle/";
            string           language         = "en-US";
            string           group            = "mygroup";
            string           topicHandle      = null;
            PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.User, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, language: language, group: group);
            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth);

            if (postTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                topicHandle = postTopicOperationResponse.Body.TopicHandle;
            }

            // create another topic
            string           topicTitle2       = "Test topic #2 for named topics";
            string           topicText2        = "This one also sure is a fine topic.";
            string           language2         = "en-US";
            string           group2            = "mygroup2";
            string           topicHandle2      = null;
            PostTopicRequest postTopicRequest2 = new PostTopicRequest(publisherType: PublisherType.User, text: topicText2, title: topicTitle2, language: language2, group: group2);
            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse2 = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest2, authorization : auth);

            if (postTopicOperationResponse2.Response.IsSuccessStatusCode)
            {
                topicHandle2 = postTopicOperationResponse2.Body.TopicHandle;
            }

            if (!(postTopicOperationResponse.Response.IsSuccessStatusCode && postTopicOperationResponse2.Response.IsSuccessStatusCode))
            {
                // if either topic creation fails, cleanup:
                // delete both topics, the admin, the user and fail the test
                await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2);

                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Create topics failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            // create a topic name for topic #1
            string topicName = "SpecialTopicName";
            PostTopicNameRequest           postTopicNameReq = new PostTopicNameRequest(publisherType: PublisherType.App, topicName: topicName, topicHandle: topicHandle);
            HttpOperationResponse <object> postTopicNameOperationResponse = await client.Topics.PostTopicNameWithHttpMessagesAsync(request : postTopicNameReq, authorization : auth);

            if (!postTopicNameOperationResponse.Response.IsSuccessStatusCode)
            {
                // if creating the topic name fails, cleanup:
                // delete both topics, the admin, the user, and fail the test
                await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2);

                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Create topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            // update the topic name so that it now refers to topic #2
            PutTopicNameRequest            putTopicNameReq = new PutTopicNameRequest(publisherType: PublisherType.App, topicHandle: topicHandle2);
            HttpOperationResponse <object> putTopicNameOperationResponse = await client.Topics.PutTopicNameWithHttpMessagesAsync(topicName : topicName, request : putTopicNameReq, authorization : auth);

            if (!putTopicNameOperationResponse.Response.IsSuccessStatusCode)
            {
                // if updating the topic name fails, cleanup
                await client.Topics.DeleteTopicNameWithHttpMessagesAsync(topicName : topicName, request : deleteRequest, authorization : auth);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

                await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2);

                ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Put topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest");
            }

            // do the standard cleanup
            await client.Topics.DeleteTopicNameWithHttpMessagesAsync(topicName : topicName, request : deleteRequest, authorization : auth);

            await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle);

            await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2);

            ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle);
            await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);
        }
        public async Task AppPublishedCreateVerifyDeleteTopicTest()
        {
            // get the app handle
            string appHandle = ManageAppsUtils.GetAppHandle(TestConstants.EnvironmentName);

            if (appHandle == null)
            {
                // fail the test
                Assert.Fail("Failed to lookup appHandle");
            }

            // Set up initial login etc
            SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl);
            var user = await TestUtilities.PostGenericUser(client);

            var auth = AuthHelper.CreateSocialPlusAuth(user.SessionToken);

            string userHandle = user.UserHandle;

            // add user as admin
            bool added = ManageAppsUtils.AddAdmin(TestConstants.EnvironmentName, appHandle, userHandle);

            if (!added)
            {
                // delete the user and fail the test
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to set user as administrator");
            }

            // create a topic
            string           topicTitle       = "My Favorite Topic";
            string           topicText        = "It is all about sports!";
            BlobType         blobType         = BlobType.Image;
            string           blobHandle       = "http://myBlobHandle/";
            string           language         = "en-US";
            string           deepLink         = "Sports!";
            string           categories       = "sports, ncurrency";
            string           group            = "mygroup";
            PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.App, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, categories: categories, language: language, deepLink: deepLink, group: group);
            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth);

            // if create topic was a success, grab the topic handle, the topic, and delete the topic and user to cleanup
            string topicHandle = string.Empty;
            HttpOperationResponse <TopicView> getTopicOperationResponse    = null;
            HttpOperationResponse <object>    deleteTopicOperationResponse = null;
            HttpOperationResponse <object>    deleteUserOperationResponse  = null;
            bool?deleteAdminResult = null;

            if (postTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                topicHandle = postTopicOperationResponse.Body.TopicHandle;
                getTopicOperationResponse = await client.Topics.GetTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);

                deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : auth);
            }

            // otherwise, delete the admin and the user
            deleteAdminResult           = ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
            deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

            // check everything went well
            Assert.IsTrue(postTopicOperationResponse.Response.IsSuccessStatusCode);
            Assert.IsTrue(getTopicOperationResponse.Response.IsSuccessStatusCode);
            Assert.IsTrue(deleteTopicOperationResponse.Response.IsSuccessStatusCode);
            Assert.IsTrue(deleteAdminResult.HasValue);
            Assert.IsTrue(deleteAdminResult.Value);
            Assert.IsTrue(deleteUserOperationResponse.Response.IsSuccessStatusCode);
            TopicView getTopicResponse = getTopicOperationResponse.Body;

            Assert.AreEqual(getTopicResponse.BlobHandle, blobHandle);
            Assert.AreEqual(getTopicResponse.BlobType, blobType);
            if (getTopicResponse.BlobUrl.Contains("images/" + blobHandle) == false)
            {
                Assert.Fail(blobHandle + "should be contained in this: " + getTopicResponse.BlobUrl);
            }

            Assert.AreEqual(getTopicResponse.Categories, categories);
            Assert.IsTrue(getTopicResponse.ContentStatus == ContentStatus.Active || getTopicResponse.ContentStatus == ContentStatus.Clean);
            Assert.AreEqual(getTopicResponse.DeepLink, deepLink);
            Assert.AreEqual(getTopicResponse.Group, group);
            Assert.AreEqual(getTopicResponse.Language, language);
            Assert.AreEqual(getTopicResponse.Liked, false);
            Assert.AreEqual(getTopicResponse.Pinned, false);
            Assert.AreEqual(getTopicResponse.PublisherType, PublisherType.App);
            Assert.AreEqual(getTopicResponse.Text, topicText);
            Assert.AreEqual(getTopicResponse.Title, topicTitle);
            Assert.AreEqual(getTopicResponse.TopicHandle, topicHandle);
            Assert.AreEqual(getTopicResponse.TotalComments, 0);
            Assert.AreEqual(getTopicResponse.TotalLikes, 0);

            // for app-published topics, the user is null
            Assert.AreEqual(getTopicResponse.User, null);
        }
        public async Task AppPublishedTopicSearchTest()
        {
            // create the client
            SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl);

            // get the app handle
            string appHandle = ManageAppsUtils.GetAppHandle(TestConstants.EnvironmentName);

            if (string.IsNullOrWhiteSpace(appHandle))
            {
                // fail the test
                Assert.Fail("Failed to lookup appHandle");
            }

            // create a user
            var user = await TestUtilities.PostGenericUser(client);

            var auth = AuthHelper.CreateSocialPlusAuth(user.SessionToken);

            // get the user handle
            string userHandle = user.UserHandle;

            if (string.IsNullOrWhiteSpace(userHandle))
            {
                // fail the test
                Assert.Fail("Failed to get userHandle");
            }

            // elevate the user to admin
            bool added = ManageAppsUtils.AddAdmin(TestConstants.EnvironmentName, appHandle, userHandle);

            if (!added)
            {
                // delete the user and fail the test
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to set user as administrator");
            }

            // create a unique string to search on
            string unique = Guid.NewGuid().ToString().Replace("-", string.Empty);

            // post a topic published by the app
            string           topicTitle       = unique;
            string           topicText        = "Something";
            BlobType         blobType         = BlobType.Unknown;
            string           blobHandle       = string.Empty;
            string           language         = "en-US";
            string           deepLink         = string.Empty;
            string           categories       = string.Empty;
            string           friendlyName     = string.Empty;
            string           group            = string.Empty;
            PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.App, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, language: language, deepLink: deepLink, categories: categories, friendlyName: friendlyName, group: group);
            HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth);

            // If the post topic operation failed, clean up
            if (postTopicOperationResponse == null || !postTopicOperationResponse.Response.IsSuccessStatusCode ||
                postTopicOperationResponse.Body == null || string.IsNullOrWhiteSpace(postTopicOperationResponse.Body.TopicHandle))
            {
                ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
                await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

                Assert.Fail("Failed to post topic");
            }

            // Delay a bit to allow data to get into the search
            await Task.Delay(TestConstants.SearchDelay);

            // search for the single result
            HttpOperationResponse <FeedResponseTopicView> search = await client.Search.GetTopicsWithHttpMessagesAsync(query : unique, cursor : null, limit : 5, authorization : auth);

            // Clean up topic
            HttpOperationResponse <object> deleteTopic = await client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : postTopicOperationResponse.Body.TopicHandle, authorization : auth);

            // Clean up first user
            bool deleteAdminResult = ManageAppsUtils.DeleteAdmin(TestConstants.EnvironmentName, appHandle, userHandle);
            HttpOperationResponse <object> deleteUser = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth);

            // Verify search result
            Assert.IsNotNull(search);
            Assert.IsNotNull(search.Body);
            Assert.IsNotNull(search.Body.Data);
            Assert.AreEqual(1, search.Body.Data.Count);
            Assert.AreEqual(postTopicOperationResponse.Body.TopicHandle, search.Body.Data[0].TopicHandle);
            Assert.AreEqual(unique, search.Body.Data[0].Title);

            // Verify deletions
            Assert.IsNotNull(deleteTopic);
            Assert.IsTrue(deleteTopic.Response.IsSuccessStatusCode);
            Assert.IsTrue(deleteAdminResult);
            Assert.IsNotNull(deleteUser);
            Assert.IsTrue(deleteUser.Response.IsSuccessStatusCode);
        }