All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.peachapisecurity.tg.junit4.examples.TestUser Maven / Gradle / Ivy

The newest version!

/*
 * Copyright 2017 Peach Tech
 * Licensed under the Apache License, Version 2.0 (the "License"); you may 
 * not use this file except in compliance with the License. You may obtain 
 * a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package com.peachapisecurity.tg.junit4.examples;

import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;

/**
 * Example JUnit4 tests for Flask Rest Target
 */
public class TestUser {
    
    private static String _baseUrl = null;
    private int _lastUserId = 2;
    private static String _apiToken = "Token b5638ae7-6e77-4585-b035-7d9de2e3f6b3";
    public static HttpHost Proxy = null;
    
    @BeforeClass
    public static void setupClass() throws Throwable
    {
        _baseUrl = System.getenv("TARGET_URL");
    }
    
    @Before
    public void setup() throws Throwable
    {
        Request
            .Get(String.format("%s/api/cleandb", _baseUrl))
            .addHeader("Authorization", _apiToken)
            .viaProxy(Proxy)
            .execute()
            .returnResponse();
    }
    
    @After
    public void teardown() throws Throwable
    {
        // Place any post-test cleanup here
    }
    
    @Test
    public void createUser() throws Throwable
    {
        JSONObject user = new JSONObject();
        user.put("user", "dd");
        user.put("first", "mike");
        user.put("last", "smith");
        user.put("password", "fnord");
        
        HttpResponse ret = Request
            .Post(String.format("%s/api/users", _baseUrl))
            .addHeader("Authorization", _apiToken)
            .viaProxy(Proxy)
            .bodyString(user.toString(), ContentType.APPLICATION_JSON)
            .execute()
            .returnResponse();
        
        Assert.assertEquals(201, ret.getStatusLine().getStatusCode());
        
        JSONObject msg = new JSONObject(EntityUtils.toString(ret.getEntity()));
        _lastUserId = msg.getInt("user_id");
    }
    
    @Test
    public void updateUser() throws Throwable
    {
        JSONObject user = new JSONObject();
        user.put("user", "dd");
        user.put("first", "mike");
        user.put("last", "smith");
        user.put("password", "fnord");
        
        HttpResponse ret = Request
            .Post(String.format("%s/api/users", _baseUrl))
            .addHeader("Authorization", _apiToken)
            .viaProxy(Proxy)
            .bodyString(user.toString(), ContentType.APPLICATION_JSON)
            .execute()
            .returnResponse();
        
        Assert.assertEquals(201, ret.getStatusLine().getStatusCode());
        
        JSONObject msg = new JSONObject(EntityUtils.toString(ret.getEntity()));
        _lastUserId = msg.getInt("user_id");
        
        user = new JSONObject();
        user.put("user", "dd");
        user.put("first", "john");
        user.put("last", "smith");
        user.put("password", "fn0rd");
        
        ret = Request
            .Put(String.format("%s/api/users/%s", _baseUrl, _lastUserId))
            .addHeader("Authorization", _apiToken)
            .viaProxy(Proxy)
            .bodyString(user.toString(), ContentType.APPLICATION_JSON)
            .execute()
            .returnResponse();
        
        Assert.assertEquals(204, ret.getStatusLine().getStatusCode());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy