1

I'm a french guy and I'm working on Django. I beggin and I have a error.

def setUp(self):
    self.username = 'admin'
    self.password = 'admin'
    self.datetime = 'Tue, 15 Nov 1994 08:12:31 GMT'
    self.temperature = '15.6'
    self.presence = '56'


def test_presence_post(self):
    #frame = 'presence=' + self.presence + '&datetime=2014-12-12 16:45:45'

    c = Client()
    c.login(username=self.username, password=self.password)
    response = c.post('/datapresence', {'presence=' + self.presence + '&datetime=2014-12-12 16:45:45'}, 
        content_type="application/x-www-form-urlencoded", HTTP_DATE=datetime)
    self.assertEqual(response.status_code, 201)

My error :
self.assertEqual(response.status_code, 201) AssertionError: 400 != 201

If i use Advanced Rest Client, it's OK with a POST request with the url and data.

Thank you very much.

Xam
  • 11
  • 2
  • Where do you create the admin user you're trying to authenticate in your test database? – Garry Cairns Apr 06 '15 at 13:56
  • 400 implied bad request. Your data seems to be malformed. Just check the format of the post dictionary sent into the `datapresence` view – karthikr Apr 06 '15 at 14:02
  • @GarryCairns My setUp is not where I create the user ? – Xam Apr 06 '15 at 14:24
  • @karthikr I have this for my datapresence formclass dataPresenceForm(forms.Form): datetime = forms.DateTimeField() presence = forms.IntegerField() – Xam Apr 06 '15 at 14:27
  • Actually, that would be fine. I am asking you to check the `request.POST` dictionary in the view – karthikr Apr 06 '15 at 14:29
  • Where is that ? I have only that for datapresence form = dataPresenceForm(request.POST) if not form.is_valid(): return HttpResponse(status=400, content=form.errors.as_text()) data = form.cleaned_data instance = BruteMesure.objects.create( datetime=data['datetime'], presence=data['presence'], ) return HttpResponse(status=201, content=instance.id) – Xam Apr 06 '15 at 14:35
  • @karthikr is it this ? – Xam Apr 06 '15 at 15:04
  • Correct.. do you know how to use pdb ? `import pdb; pdb.set_trace()` and see the contents of `request.POST.copy()` – karthikr Apr 06 '15 at 15:08
  • @karthikr I don't know how to see the contents of request.POST.copy()...sorry ! – Xam Apr 06 '15 at 15:43

1 Answers1

1

{'presence=' + self.presence + '&datetime=2014-12-12 16:45:45'} is a set with a single key and no values, not a dictionary as expected by post(). Should be {'presence': self.presence, 'datetime':'2014-12-12 16:45:45'}. The server detects an invalid request and returns 400.

I would also try without content_type="application/x-www-form-urlencoded", HTTP_DATE=datetime, the default values should be at least as good as the ones provided.

Mike Bessonov
  • 676
  • 3
  • 8
  • Without my values, I have this : IntegrityError: NOT NULL constraint failed: IceApp_brutemesure.temperature – Xam Apr 06 '15 at 16:19