{"size": 1709, "ext": "js", "max_stars_repo_path": "posts/post-router.spec.js", "max_stars_repo_name": "LambdaSchool-Post-Here-1/back-end", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "posts/post-router.spec.js", "max_issues_repo_name": "LambdaSchool-Post-Here-1/back-end", "max_issues_repo_issues_event_min_datetime": "2021-09-02T11:55:13.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T11:55:13.000Z", "max_forks_repo_path": "posts/post-router.spec.js", "max_forks_repo_name": "LambdaSchool-Post-Here-1/back-end", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 29.9824561404, "max_line_length": 182, "alphanum_fraction": 0.6968987712}
const supertest = require("supertest")
const server = require('../api/server')
const jwt = require("jsonwebtoken")
const configVars = require('../config/vars')

// const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjIsInVzZXJuYW1lIjoibGFycyIsImlhdCI6MTU5MDYxNzYwNCwiZXhwIjoxNTkwNzA0MDA0fQ.OYn8A2ukafm0ySjtJ4ly49lBhAEGWNRil6lLuPrY5OQ"

const token = createToken({username:'lars',password:'drums'})


describe('GET to /api/reddit', ()=>{
  it('should send authentication instructions', ()=>{
    return supertest(server).get('/api/reddit')
    .then(response =>{ 
      // console.log("response body",response.body)
      expect(response.body).toHaveProperty("message", "Please provide authentication information") // object that requests authentication information
      // expect(response.body) // object that requests authentication information

    })
  })


  it('should return an array', ()=>{
    return supertest(server).get('/api/reddit')
    .set('Authorization', token)
    .then(response =>{ 
      expect(Array.isArray(response.body)).toBe(true) // object that requests authentication information
      expect(response.body).toHaveLength(4)
      console.log("Response Body of GET request", response.body)
    })
  })

  it('should return an array', ()=>{
    return supertest(server).get('/api/reddit/3')
    .set('Authorization', token)
    .then(response =>{ 
      expect(typeof response.body).toBe("object")
      expect(response.body).toHaveProperty("author")

    })
  })
})



function createToken(user)
{
  const payload = {
    sub: user.id,
    username: user.username
  }
  const options = {
    expiresIn: '1d'
  }

  return jwt.sign(payload, configVars.jwtSecret, options)
}