Skip to content

Commit

Permalink
Update the README and add a test for the example in the README.
Browse files Browse the repository at this point in the history
Bump the version number.
  • Loading branch information
braveulysses committed Feb 10, 2017
1 parent ae1bd27 commit 69c6985
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 11 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.1] - 2017-02-10
This release fixes a bug in which functions from the Patch module weren't properly exported.

## [0.1.0] - 2017-01-24
Initial release.

## [0.1.1] - 2017-xx-xx
Version 0.1.1 notes go here.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,37 @@

This is a simple JavaScript library for [SCIM 2](http://www.simplecloud.info/) applications. SCIM 2 is a set of standards that defines a schema for representing identities and a REST API for creating, retrieving, updating, and deleting them.

The feature set is not exhaustive and is intended to cover a limited set of common cases. These include:
This library's feature set is not exhaustive and covers a limited set of common cases. These include:

* Parsing a JSON document into a SCIM resource object.
* Getting attribute values from a SCIM object using SCIM paths like `userName` or `emails[value eq "user@example.com"]`. (Not supported: filters containing multiple expressions)
* Modifying a SCIM resource object.
* Building a PATCH request.

This library will help you parse SCIM responses and formulate certain kinds of requests, but it doesn't actually provide an HTTP client, since numerous generic options are already available and will work well. For example, see [Axios](https://github.com/mzabriskie/axios), [whatwg-fetch](https://github.com/github/fetch), or [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch).
This library will help you parse SCIM responses and formulate certain kinds of requests, but it doesn't actually provide an HTTP client, since numerous generic options are already available that work well. For example, see [Axios](https://github.com/mzabriskie/axios), [whatwg-fetch](https://github.com/github/fetch), or [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch).

If you're looking for a SCIM 2 library with more comprehensive support for the SCIM 2 standard, see the [UnboundID SCIM 2 SDK for Java](https://github.com/UnboundID/scim2). I'm not aware of a comparable JavaScript SDK.

## Installation

```
npm install @braveulysses/scim2-js
```

Or

```
yarn add @braveulysses/scim2-js
```

## Example

```javascript
import { Resource, Patch, Constants } from 'scim2-js';

const url = 'https://example.com/scim/v2/Users/7f93b9ae-85ce-4bce-baf8-1c18f25ace64';
const accessToken = '...';
const expectedUsername = '...';

// This example uses the Fetch API, but the request mechanism is up to you.
fetch(url, {
Expand All @@ -33,9 +47,9 @@ fetch(url, {
return response.json();
}).then(function(json) {
const user = new Resource(json);
console.log(user.id());
console.log(user.schemas());
console.log(user.get('userName'));
expect(user.id()).toEqual('7f93b9ae-85ce-4bce-baf8-1c18f25ace64');
expect(user.schemas()).toContain('urn:ietf:params:scim:schemas:core:2.0:User');
expect(user.get('userName')).toEqual(expectedUsername);

// Modify an attribute.
const phoneNumbers = user.get('phoneNumbers');
Expand All @@ -46,6 +60,7 @@ fetch(url, {
});
user.set('phoneNumbers', phoneNumbers);
// You could then apply the modification by replacing the resource with PUT.
expect(user.get('phoneNumbers')).toEqual(phoneNumbers);

// Alternatively, you could build a partial modification request...
const patch = Patch.patchRequest(
Expand All @@ -56,8 +71,6 @@ fetch(url, {
})
);
// ... and then apply the modification by sending a PATCH request.

console.log(user.get('phoneNumbers[type eq "other"]'));
});
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@braveulysses/scim2",
"version": "0.1.1",
"version": "0.1.2",
"description": "Simple SCIM 2 for JavaScript",
"main": "./dist/index.js",
"scripts": {
Expand Down
137 changes: 137 additions & 0 deletions src/__tests__/README.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { Resource, Patch, Constants } from '../index';

describe('The README example', () => {
const RESOURCE = `
{
"schemas": [
"urn:example.com:scim:schemas:example:1.0",
"urn:ietf:params:scim:schemas:core:2.0:User"
],
"id": "7f93b9ae-85ce-4bce-baf8-1c18f25ace64",
"meta": {
"created": "2016-11-16T17:14:57.836Z",
"lastModified": "2016-11-19T18:24:27.433Z",
"resourceType": "Users",
"location": "https://example.com/scim/v2/Users/7f93b9ae-85ce-4bce-baf8-1c18f25ace64"
},
"userName": "rkn",
"enabled": true,
"name": {
"familyName": "Narayanaswami",
"formatted": "R.K. Narayan",
"givenName": "Rasipuram"
},
"emails": [
{
"value": "rk.narayan@gmail.com",
"primary": true,
"type": "home"
},
{
"value": "rknarayan@example.com",
"primary": false,
"type": "work"
}
],
"phoneNumbers": [
{
"value": "+1 361 720 7250",
"type": "mobile",
"primary": true
},
{
"value": "+1 361 456 3207",
"type": "work",
"primary": false
},
{
"value": "+1 361 720 7251",
"type": "home",
"primary": false
}
],
"addresses": [
{
"streetAddress": "85287 Church Street",
"locality": "Toledo",
"region": "RI",
"postalCode": "02824",
"country": "US",
"primary": true,
"type": "home"
}
],
"urn:example.com:scim:schemas:example:1.0": {
"birthDate": "1906-10-10",
"cats": [
{
"name": "Ravana",
"color": "black"
},
{
"name": "Hanuman",
"color": "orange tabby"
}
]
}
}
`;

class Response {
constructor(obj) {
this.obj = obj;
this.json = this.json.bind(this);
}

json() {
return Promise.resolve(this.obj);
}
}

const fetch = (url, options) => {
const response = new Response(JSON.parse(RESOURCE));
return Promise.resolve(response);
};

it('Actually works', () => {
expect(Constants.SCIM_MEDIA_TYPE).toEqual('application/scim+json');

const url = 'https://example.com/scim/v2/Users/7f93b9ae-85ce-4bce-baf8-1c18f25ace64';
const accessToken = '...';
const expectedUsername = 'rkn';

fetch(url, {
method: 'get',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Accept': Constants.SCIM_MEDIA_TYPE
}
}).then(function(response) {
return response.json();
}).then(function(json) {
const user = new Resource(json);
expect(user.id()).toEqual('7f93b9ae-85ce-4bce-baf8-1c18f25ace64');
expect(user.schemas()).toContain('urn:ietf:params:scim:schemas:core:2.0:User');
expect(user.get('userName')).toEqual(expectedUsername);

const phoneNumbers = user.get('phoneNumbers');
phoneNumbers.push({
value: "+1 555 555 1234",
type: "other",
primary: false
});
user.set('phoneNumbers', phoneNumbers);
expect(user.get('phoneNumbers')).toEqual(phoneNumbers);

const patch = Patch.patchRequest(
Patch.addOperation('phoneNumbers', {
value: "+1 555 555 1234",
type: "other",
primary: false
})
);
const expectedPatch = `{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"add","path":"phoneNumbers","value":{"value":"+1 555 555 1234","type":"other","primary":false}}]}`;
expect(JSON.parse(patch)).toEqual(JSON.parse(expectedPatch));
});
});
});

0 comments on commit 69c6985

Please sign in to comment.