Coder Social home page Coder Social logo

Frisby.js and OAuth? about frisby HOT 5 CLOSED

vlucas avatar vlucas commented on August 29, 2024
Frisby.js and OAuth?

from frisby.

Comments (5)

vlucas avatar vlucas commented on August 29, 2024

There are no examples currently, but OAuth would just be a series of requests that run after the previous one using the after or afterJSON callback methods.

from frisby.

andyshinn avatar andyshinn commented on August 29, 2024

We just started running into the same issue (having to get our OAuth2 token before testing endpoints). afterJSON is working for us. Here is an example:

frisby.create('OAuth2 login')
  .post('https://oauth.ourapi.com/access', {
    username: 'someuser',
    password: 'somepass',
    client_id: 'someid',
    client_sig: 'somesig',
    grant_type: 'password'
  })
  .expectStatus(200)
  .afterJSON(function(response) {
    frisby.create('Delete user')
      .delete('https://api.ourapi.com/users/me')
      .addHeader('Authorization', 'Bearer ' + response.access_token)
      .expectStatus(204)
      .toss();
  })
  .toss();

However, I think there might be a bug in either Frisby or Jasmine, it doesn't actually output the test that was run. But, if there is a failure, it does still error and show the failure.

Hope this helps!

from frisby.

joevallender avatar joevallender commented on August 29, 2024

This is top result for a search of "frisby oauth" so I thought I'd drop an example that is working for me.

In case it's of interest. This is testing an API implemented using Drupal 7, OAuth2 server, Services 3 (REST server). In this case, username is client_id and password is client_secret

var frisby         = require('frisby');

var domain         = 'https://my.domain.com/',
    oauthTokenUrl  = domain + 'oauth2/token',
    apiPath        = domain + 'api/v2/',
    accountUrl     = apiPath + 'account',
    username       = 'username',
    password       = 'password',
    auth           = "Basic " + new Buffer(username + ":" + password).toString("base64");

var accountId      = '12345',
    username       = 'joe';

frisby.create('OAuth2 login')
  .addHeader('Authorization', auth)
  .addHeader('Accept', 'application/json')
  .post(oauthTokenUrl, {
    grant_type: 'client_credentials'
  })
  .expectStatus(200)
  .afterJSON(function(response) {

    frisby.create('Get account')
      .get(accountUrl + '/' + accountId)
      .addHeader('Authorization', 'Bearer ' + response.access_token)
      .expectStatus(200)
      .afterJSON(function(account){
        expect(account.username).toEqual(username);
      })
      .toss();

  })
  .toss();

from frisby.

vlucas avatar vlucas commented on August 29, 2024

Thanks @joevallender - I will have to add this example to the docs.

from frisby.

dagumak avatar dagumak commented on August 29, 2024

@joevallender Thanks for the example. Just out of curiosity, how are you keeping your test database clean of your test data? I come from Rails, so fixtures and test data are pretty much obliterated and reloaded on every load.

from frisby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.