Ejemplo n.º 1
0
        public async Task Post_Should_Create_Ressource_Server()
        {
            var scopeIsReadWrite = true;

            var toCreateRessourceServer = new CreateRessourceServerDto()
            {
                Description    = "new ressource server",
                Login          = "******",
                Name           = "newRs",
                Password       = "******",
                RepeatPassword = "******",
                Scopes         = new List <CreateRessourceServerScopesDto>()
                {
                    new CreateRessourceServerScopesDto()
                    {
                        IsReadWrite = scopeIsReadWrite,
                        NiceWording = "a new scope"
                    }
                }
            };

            var httpResponseMessage = await _client.PostAsJsonAsync("ressourcesServers", toCreateRessourceServer);

            Assert.AreEqual(HttpStatusCode.Created, httpResponseMessage.StatusCode);
            Assert.IsTrue(httpResponseMessage.Headers.Contains("location"));

            var location = httpResponseMessage.Headers.GetValues("location").Single();

            Assert.IsTrue(location.Length > 0);

            var ressourceServerId = Int32.Parse(location.Split('/', StringSplitOptions.RemoveEmptyEntries).Last());

            RessourceServer myNewRessourceServer = null;
            Scope           myNewScopes          = null;

            using (var context = new DaOAuthContext(_dbContextOptions))
            {
                myNewRessourceServer = context.RessourceServers.Where(c => c.Id.Equals(ressourceServerId)).SingleOrDefault();
                myNewScopes          = context.Scopes.Where(s => s.RessourceServerId.Equals(ressourceServerId)).SingleOrDefault();
            }

            Assert.IsNotNull(myNewRessourceServer);

            Assert.AreEqual(toCreateRessourceServer.Description, myNewRessourceServer.Description);
            Assert.AreEqual(toCreateRessourceServer.Login, myNewRessourceServer.Login);
            Assert.AreEqual(toCreateRessourceServer.Name, myNewRessourceServer.Name);

            var encryptionServce = new EncryptionService();

            Assert.IsTrue(encryptionServce.AreEqualsSha256(
                              String.Concat(GuiApiTestStartup.Configuration.PasswordSalt, toCreateRessourceServer.Password),
                              myNewRessourceServer.ServerSecret));

            Assert.IsTrue(myNewRessourceServer.IsValid);
            Assert.AreEqual(DateTime.Now.Date, myNewRessourceServer.CreationDate.Date);

            Assert.IsNotNull(myNewScopes);
            Assert.AreEqual(toCreateRessourceServer.Scopes.Single().NiceWording, myNewScopes.NiceWording);
            Assert.AreEqual(ressourceServerId, myNewScopes.RessourceServerId);
            Assert.IsTrue(myNewScopes.Wording.Length > 0);

            if (scopeIsReadWrite)
            {
                Assert.IsTrue(myNewScopes.Wording.StartsWith("RW_"));
            }
            else
            {
                Assert.IsTrue(myNewScopes.Wording.StartsWith("R_"));
            }
        }