Exemplo n.º 1
0
        internal Guid SaveThroughGraphQl(BaseEntity model)
        {
            var api   = new WebApi(_configure);
            var query = QueryBuilder.CreateEntityQueryBuilder(new List <BaseEntity> {
                model
            });

            api.ConfigureAuthenticationHeaders();

            if (model is IFileContainingEntity fileContainingEntity)
            {
                var headers = new Dictionary <string, string> {
                    { "Content-Type", "multipart/form-data" }
                };
                var files = fileContainingEntity.GetFiles().Where(file => file != null);;
                var param = new Dictionary <string, object>
                {
                    { "operationName", query["operationName"] },
                    { "variables", query["variables"] },
                    { "query", query["query"] }
                };

                api.Post($"/api/graphql", param, headers, DataFormat.None, files);
                return(Id);
            }
            api.Post($"/api/graphql", query);
            return(Id);
        }
Exemplo n.º 2
0
        public void ApiDeleteEntities(EntityFactory entityFactory)
        {
            // create some test entities over the api to run the delete tests against
            var entityList = entityFactory.ConstructAndSave(_output, 1);

            foreach (var entityObject in entityList)
            {
                // instantiate a list of entity names and guids to be deleted
                var entityKeysGuids = new List <KeyValuePair <string, Guid> >();
                entityKeysGuids.Add(new KeyValuePair <string, Guid>(entityObject.EntityName, entityObject.Id));

                // populate the list using information returned from create entities.
                GetParentKeysGuids(entityObject).ForEach(x => entityKeysGuids.Add(x));

                foreach (var entityKeyGuid in entityKeysGuids)
                {
                    // instantiate a new rest client, and configure the Uri

                    var endPoint = $"/api/entity/{entityKeyGuid.Key}/{entityKeyGuid.Value}";
                    var api      = new WebApi(_configure, _output);
                    api.ConfigureAuthenticationHeaders();

                    Assert.Equal(HttpStatusCode.OK, api.Get(endPoint).StatusCode);
                    Assert.Equal(HttpStatusCode.OK, api.Delete(endPoint).StatusCode);
                    Assert.Equal(HttpStatusCode.NoContent, api.Get(endPoint).StatusCode);
                    Assert.Equal(HttpStatusCode.OK, api.Delete(endPoint).StatusCode);
                }
            }
        }
Exemplo n.º 3
0
        public void TestApiEndPoints(string entityName)
        {
            var api = new WebApi(_configure, _output);

            api.ConfigureAuthenticationHeaders();
            var response = api.Get($"/api/entity/{entityName}");

            // a valid response code
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Exemplo n.º 4
0
        public void ExportEntity(EntityFactory entityFactory, int numEntities)
        {
            var entityList = entityFactory.ConstructAndSave(_output, numEntities);
            var entityName = entityList[0].EntityName;

            var api = new WebApi(_configure, _output);


            var query     = QueryBuilder.CreateExportQuery(entityList);
            var queryList = new JsonArray {
                new JsonArray {
                    query
                }
            };

            api.ConfigureAuthenticationHeaders();
            var response = api.Post($"/api/entity/{entityName}/export", queryList);

            var responseDictionary = CsvToDictionary(response.Content)
                                     .ToDictionary(pair => pair.Key.ToLowerInvariant(), pair => pair.Value);

            foreach (var entity in entityList)
            {
                var entityDict = entity.ToDictionary()
                                 .ToDictionary(pair => pair.Key.ToLowerInvariant(), pair => pair.Value);

                if (entity is UserBaseEntity)
                {
                    // export will not contain password
                    entityDict.Remove("password");
                }

                if (entity is IFileContainingEntity)
                {
                    // file ids are generated server-side
                    foreach (var fileAttributeKey in GetFileEntityFilterKeys(entity))
                    {
                        entityDict.Remove(fileAttributeKey);
                    }
                }

                foreach (var attributeKey in entityDict.Keys.Select(x => x.ToLowerInvariant()))
                {
                    responseDictionary.Should().ContainKey(attributeKey);
                    responseDictionary[attributeKey]
                    .Should()
                    .Contain(entityDict[attributeKey])
                    .And
                    .HaveCount(numEntities);
                }
            }
        }
Exemplo n.º 5
0
        public void TestGraphqlEndPoints(string entityName)
        {
            var api = new WebApi(_configure, _output);

            var query = new RestSharp.JsonObject();

            query.Add("query", "{ " + entityName + "{id}}");

            api.ConfigureAuthenticationHeaders();
            var response = api.Post($"/api/graphql", query);

            //check the ids are valid
            var validIds = JObject.Parse(response.Content)["data"][entityName]
                           .Select(o => o["id"].Value <string>())
                           .All(o => !string.IsNullOrWhiteSpace(o));

            //valid ids returned and a valid response
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Exemplo n.º 6
0
        /// <summary>
        /// This function is called recursively to delete
        /// entities, it is abstracted away from the creation tests
        /// so it can run independently
        /// </summary>
        /// <param name="entityList">
        /// Takes a list of entities as input, these entities are delete and their
        /// parent entities are also deleted.
        /// </param>
        internal void GraphQlDelete(List <BaseEntity> entityList)
        {
            var api = new WebApi(_configure, _output);

            // form the query to delete the entity
            // mass delete all entities in the list in a single request and check status code is ok
            var deleteQuery = QueryBuilder.DeleteEntityQueryBuilder(entityList);

            api.ConfigureAuthenticationHeaders();
            api.Post($"/api/graphql", deleteQuery);

            /*
             * Run recursively for parent entities,
             * The first item is passed through because each entity in
             * the list share the same parent references
             */
            foreach (var parentEntity in entityList[0].ParentEntities)
            {
                GraphQlDelete(new List <BaseEntity>()
                {
                    parentEntity
                });
            }
        }
Exemplo n.º 7
0
        public List <BaseEntity> CreateEntities(EntityFactory entityFactory, int numEntities)
        {
            // get the list of entities we will be creating
            var entityList = CreateReferencedEntities(entityFactory, numEntities);

            var api = new WebApi(_configure, _output);

            /*
             * get the first entity out of the list, we will be iterating over this one and creating the references needed
             * for it. This means that all the created entities share the same references
             */
            var query = QueryBuilder.CreateEntityQueryBuilder(entityList);

            api.ConfigureAuthenticationHeaders();

            var response = PostCreateRequest(api, query, entityList);


            //valid ids returned and a valid response
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            //return the object at the end, this can be reused.
            return(entityList);
        }