2021-09-27 10:10:19 +00:00
package main_test
import (
"context"
"errors"
"flag"
2021-09-28 09:08:15 +00:00
"io/ioutil"
v1 "jokes-bapak2-api/app"
2021-09-27 10:10:19 +00:00
"jokes-bapak2-api/app/platform/database"
"log"
2021-09-28 09:08:15 +00:00
"net/http"
2021-09-27 10:10:19 +00:00
"os"
2021-09-28 09:08:15 +00:00
"strings"
2021-09-27 10:10:19 +00:00
"testing"
"time"
2021-09-28 09:08:15 +00:00
"github.com/gofiber/fiber/v2"
2021-09-27 10:10:19 +00:00
"github.com/jackc/pgx/v4/pgxpool"
2021-09-28 09:08:15 +00:00
_ "github.com/joho/godotenv/autoload"
"github.com/stretchr/testify/assert"
2021-09-27 10:10:19 +00:00
)
var jokesData = [ ] interface { } { 1 , "https://via.placeholder.com/300/06f/fff.png" , 1 , 2 , "https://via.placeholder.com/300/07f/fff.png" , 1 , 3 , "https://via.placeholder.com/300/08f/fff.png" , 1 }
var submissionData = [ ] interface { } { 1 , "https://via.placeholder.com/300/01f/fff.png" , "2021-08-03T18:20:38Z" , "Test <test@example.com>" , 0 , 2 , "https://via.placeholder.com/300/02f/fff.png" , "2021-08-04T18:20:38Z" , "Test <test@example.com>" , 1 }
var administratorsData = [ ] interface { } { 1 , "very secure" , "not the real one" , time . Now ( ) . Format ( time . RFC3339 ) , 2 , "test" , "$argon2id$v=19$m=65536,t=16,p=4$3a08c79fbf2222467a623df9a9ebf75802c65a4f9be36eb1df2f5d2052d53cb7$ce434bd38f7ba1fc1f2eb773afb8a1f7f2dad49140803ac6cb9d7256ce9826fb3b4afa1e2488da511c852fc6c33a76d5657eba6298a8e49d617b9972645b7106" , "" }
var ctx context . Context = context . Background ( )
func TestMain ( m * testing . M ) {
flag . Parse ( )
2021-09-28 09:08:15 +00:00
2021-09-27 13:02:58 +00:00
log . Println ( "---- Preparing for integration test" )
2021-09-28 09:08:15 +00:00
time . Sleep ( time . Second * 5 )
2021-09-27 10:10:19 +00:00
err := setup ( )
if err != nil {
2021-09-28 04:29:39 +00:00
log . Panicln ( err )
2021-09-27 10:10:19 +00:00
}
2021-09-28 09:08:15 +00:00
time . Sleep ( time . Second * 5 )
2021-09-27 13:02:58 +00:00
log . Println ( "---- Preparation complete" )
log . Print ( "\n" )
2021-09-28 09:08:15 +00:00
os . Exit ( m . Run ( ) )
2021-09-27 10:10:19 +00:00
}
func setup ( ) error {
poolConfig , err := pgxpool . ParseConfig ( os . Getenv ( "DATABASE_URL" ) )
if err != nil {
return errors . New ( "Unable to create pool config: " + err . Error ( ) )
}
2021-09-27 13:02:58 +00:00
db , err := pgxpool . ConnectConfig ( ctx , poolConfig )
2021-09-27 10:10:19 +00:00
if err != nil {
return errors . New ( "Unable to create connection: " + err . Error ( ) )
}
2021-09-28 09:08:15 +00:00
dj , err := db . Query ( ctx , "DROP TABLE IF EXISTS \"jokesbapak2\"" )
2021-09-28 04:29:54 +00:00
if err != nil {
return err
}
2021-09-28 09:08:15 +00:00
dj . Close ( )
2021-09-28 04:29:39 +00:00
2021-09-28 09:08:15 +00:00
ds , err := db . Query ( ctx , "DROP TABLE IF EXISTS \"submission\"" )
2021-09-28 04:29:54 +00:00
if err != nil {
return err
}
2021-09-28 09:08:15 +00:00
ds . Close ( )
2021-09-28 04:29:39 +00:00
2021-09-28 09:08:15 +00:00
da , err := db . Query ( ctx , "DROP TABLE IF EXISTS \"administrators\"" )
2021-09-27 10:10:19 +00:00
if err != nil {
return err
}
2021-09-28 09:08:15 +00:00
da . Close ( )
2021-09-27 10:10:19 +00:00
err = database . Setup ( db , & ctx )
if err != nil {
return err
}
2021-09-28 04:29:39 +00:00
ia , err := db . Query ( ctx , "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);" , administratorsData ... )
2021-09-27 10:10:19 +00:00
if err != nil {
return err
}
2021-09-28 09:08:15 +00:00
ia . Close ( )
2021-09-27 10:10:19 +00:00
2021-09-28 04:29:39 +00:00
ij , err := db . Query ( ctx , "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);" , jokesData ... )
2021-09-27 10:10:19 +00:00
if err != nil {
return err
}
2021-09-28 09:08:15 +00:00
ij . Close ( )
2021-09-27 10:10:19 +00:00
2021-09-28 04:29:39 +00:00
is , err := db . Query ( ctx , "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);" , submissionData ... )
2021-09-27 10:10:19 +00:00
if err != nil {
return err
}
2021-09-28 09:08:15 +00:00
is . Close ( )
db . Close ( )
2021-09-27 10:10:19 +00:00
return nil
}
2021-09-28 09:08:15 +00:00
var app * fiber . App = v1 . New ( )
func TestHealth ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/health" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
if err != nil {
t . Fatal ( err )
}
assert . Equalf ( t , false , err != nil , "health" )
assert . Equalf ( t , 200 , res . StatusCode , "health" )
assert . NotEqualf ( t , 0 , res . ContentLength , "health" )
_ , err = ioutil . ReadAll ( res . Body )
if err != nil {
t . Fatal ( err )
}
defer res . Body . Close ( )
assert . Nilf ( t , err , "health" )
}
func TestTodayJoke ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/today" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "today joke" )
assert . Equalf ( t , 200 , res . StatusCode , "today joke" )
assert . NotEqualf ( t , 0 , res . ContentLength , "today joke" )
_ , err = ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "today joke" )
}
func TestSingleJoke ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "single joke" )
assert . Equalf ( t , 200 , res . StatusCode , "single joke" )
assert . NotEqualf ( t , 0 , res . ContentLength , "single joke" )
_ , err = ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "single joke" )
}
func TestJokeByID_200 ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/id/1" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "joke by id" )
assert . Equalf ( t , 200 , res . StatusCode , "joke by id" )
assert . NotEqualf ( t , 0 , res . ContentLength , "joke by id" )
_ , err = ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "joke by id" )
}
func TestJokeByID_404 ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/id/300" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "joke by id" )
assert . Equalf ( t , 404 , res . StatusCode , "joke by id" )
assert . NotEqualf ( t , 0 , res . ContentLength , "joke by id" )
body , err := ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "joke by id" )
assert . Equalf ( t , "Requested ID was not found." , string ( body ) , "joke by id" )
}
func TestTotalJokes ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/total" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "joke total" )
assert . Equalf ( t , 200 , res . StatusCode , "joke total" )
assert . NotEqualf ( t , 0 , res . ContentLength , "joke total" )
body , err := ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "joke total" )
assert . Equalf ( t , "{\"message\":\"3\"}" , string ( body ) , "joke total" )
}
func TestAddNewJoke_201 ( t * testing . T ) {
// TODO: Remove this line below, make this test works
t . SkipNow ( )
reqBody := strings . NewReader ( "{\"link\":\"https://via.placeholder.com/300/04f/ff0000.png\",\"key\":\"test\",\"token\":\"password\"}" )
req , _ := http . NewRequest ( "PUT" , "/" , reqBody )
req . Header . Set ( "content-type" , "application/json" )
req . Header . Add ( "accept" , "application/json" )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "joke add" )
assert . Equalf ( t , 201 , res . StatusCode , "joke add" )
assert . NotEqualf ( t , 0 , res . ContentLength , "joke add" )
body , err := ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "joke add" )
assert . Equalf ( t , "{\"link\":\"https://via.placeholder.com/300/04f/ff0000.png\"}" , string ( body ) , "joke add" )
}
func TestAddNewJoke_NotValidImage ( t * testing . T ) {
// TODO: Remove this line below, make this test works
t . SkipNow ( )
reqBody := strings . NewReader ( "{\"link\":\"https://google.com/\",\"key\":\"test\",\"token\":\"password\"}" )
req , _ := http . NewRequest ( "PUT" , "/" , reqBody )
req . Header . Set ( "content-type" , "application/json" )
req . Header . Add ( "accept" , "application/json" )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "joke add" )
assert . Equalf ( t , 400 , res . StatusCode , "joke add" )
body , err := ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "joke add" )
assert . Equalf ( t , "{\"error\":\"URL provided is not a valid image\"}" , string ( body ) , "joke add" )
}
func TestGetSubmission_200 ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/submit" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "get submission" )
assert . Equalf ( t , 200 , res . StatusCode , "get submission" )
body , err := ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "get submission" )
assert . Equalf ( t , "{\"count\":2,\"jokes\":[{\"id\":1,\"link\":\"https://via.placeholder.com/300/01f/fff.png\",\"created_at\":\"2021-08-03T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":0},{\"id\":2,\"link\":\"https://via.placeholder.com/300/02f/fff.png\",\"created_at\":\"2021-08-04T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":1}]}" , string ( body ) , "get submission" )
}
func TestGetSubmission_Params ( t * testing . T ) {
req , _ := http . NewRequest ( "GET" , "/submit?page=1&limit=5&approved=true" , nil )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "get submission" )
assert . Equalf ( t , 200 , res . StatusCode , "get submission" )
body , err := ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "get submission" )
assert . Equalf ( t , "{\"count\":1,\"jokes\":[{\"id\":2,\"link\":\"https://via.placeholder.com/300/02f/fff.png\",\"created_at\":\"2021-08-04T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":1}]}" , string ( body ) , "get submission" )
}
func TestAddSubmission_200 ( t * testing . T ) {
// TODO: Remove this line below, make this test works
t . Skip ( )
reqBody := strings . NewReader ( ` { "link":"https://via.placeholder.com/400/02f/fff.png","author":"Test <test@mail.com>"} ` )
req , _ := http . NewRequest ( "POST" , "/submit" , reqBody )
req . Header . Set ( "content-type" , "application/json" )
req . Header . Add ( "accept" , "application/json" )
res , err := app . Test ( req , int ( time . Minute * 2 ) )
assert . Equalf ( t , false , err != nil , "post submission" )
assert . Equalf ( t , 201 , res . StatusCode , "post submission" )
assert . NotEqualf ( t , 0 , res . ContentLength , "post submission" )
_ , err = ioutil . ReadAll ( res . Body )
defer res . Body . Close ( )
assert . Nilf ( t , err , "post submission" )
}